Im having a little problem with a peice of regex im trying to write... im a profficent coder when it comes to HTML, CSS, PHP, JavaScript and AS2/AS3 but the one thing that still trips me up again and again is regex!
Im trying to write a peice of regex that will be used with preg_replace to remove all "[" characters from a string as long as they don't appear within double (") or single (') quotes or a blackslash(\). When appearing in quotes it ([) could have any characters (a-zA-Z0-9/,.\;[]-_()*&^%$#@!`~<>?{}|) either side of it within the quotes. How would I do this? I have included the regex I tried to create to solve this problem but it doesn't quite work. I hope i'm on the right track...
Code:
[^[["|'][^"|']*\[[^"|']*["|']]]
I feel im almost on the right track but im not quite there. Any and all help would be appreciated.
Thanks,
Chris
03-24-2010, 04:16 AM
Mindzai
Someone with better regex skills can probably come up with a one liner, but here's how I'd do it:
PHP Code:
$matches = (preg_split('/([\'"].+?[\'"])/', $string, -1, PREG_SPLIT_DELIM_CAPTURE)); $new = ''; foreach ($matches as $match) { if (strpos($match, '"') === 0 || strpos($match, "'") === 0) { // stuff inside quotes can be handled here $new .= $match; continue; } // stuff outside quotes can be handled here $new .= str_replace('[', '', $match); }