So I have a string that looks like this <span>hey</span>/<span>bye</span>/<span>why</span>/
. How would I use preg_replace
to substitute all the non-HTML-tag /
s with ::
?
Asked
Active
Viewed 45 times
1 Answers
0
I would consider using negative lookbehind for this:
$pattern = '#(?<!<)/#';
$replacement = '::';
$result = preg_replace($pattern, $replacement, $input);
The gist of this is that it will only replace /
where the preceding character is not <
Of course you might need to go a step further and make sure it doesn't have a following character of >
, such as would be the case with self-closing tags. In that case, you could add a negative lookahead as well such that the pattern becomes:
$pattern = '#(?<!<)/(?!>)#';

Mike Brant
- 70,514
- 10
- 99
- 103
-
I just posted this same answer :-) – gen_Eric Jun 25 '13 at 18:19
-
*clap* *clap* *clap* was watching thread ... you posted after Mike – Avner Solomon Jun 25 '13 at 18:20