Results 1 to 2 of 2
May 15, 2007, 22:02 #1
- Join Date
- Jan 2007
- 0 Post(s)
- 0 Thread(s)
regular expression to ignore a character if another is found before it.
Lets say we have these two strings...
How can I do a preg_replace on these strings and only do a replace on the '#' character if the previous character is -NOT- &. So for instance the regular expression would only do the replace in the second string.
May 16, 2007, 01:07 #2PHP Code:
$regex = "~(?<!&)#~";
$text1 = "Ok &#";
$repl1 = preg_replace($regex, "xxx", $text1);
$text2 = "Okdsd#k";
$repl2 = preg_replace($regex, "xxx", $text2);
echo "$text1<br />$repl1<br />$text2<br />$repl2";
Never ascribe to malice,
that which can be explained by incompetence.
Your code should not look unmaintainable, just be that way.