Forum Moderators: coopster

Message Too Old, No Replies

Preg Replace Problem: Leaving out HTML

preg_replace, span, html, problem

         

starefossen

10:09 pm on Jul 27, 2009 (gmt 0)

10+ Year Member



I am having a problem with my preg replace function. Let me try to explain it.

I have the following string which holds a bunch of text:

$text = "This is a lot of text containing the word [b]span[/b] and we also have this: <[b]span[/b] style='background-color:yellow;color:#333;font-weight:bold;padding-left:2px;padding-right:2px'>some text containing [b]span[/b]</[b]span[/b]> and some other [b]span[/b] words here and there.";

I want to replace pan (not span but only pan) with div but only where it is plain text not HTML or inside tags and the wanted result should be this:

$text = "This is a lot of text containing the word [b]sdiv[/b] and we also have this: <span style='background-color:yellow;color:#333;font-weight:bold;padding-left:2px;padding-right:2px'>some text containing span</span> and some other [b]sdiv[/b] words here and there.";

I tried this preg_replace but it is not working correctly:

$text = preg_replace("/([^<\/]pan)/is", "div", $text);

Are there anyone who know how I can make this happen?

Kind regards,
Hans Kristian

jatar_k

2:34 pm on Jul 28, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



on that you could just use str_replace

$text = str_replace(' span', ' sdiv', $text);

span with a <space> before it shouldn't get any tags, though you may find an occurrence that this might not work, it works within the example