Any questions involving matching text strings to patterns - the pattern is called a "regular expression."
Moderator: General Moderators
abdz12
Forum Newbie
Posts: 2 Joined: Fri May 15, 2009 3:50 am
Post
by abdz12 » Fri May 15, 2009 7:25 am
hi all
I'm having a hard time replacing words "only", that is, do not replace occurences of the word in an HTML-tag. Here's some code: PHP Code:
Code: Select all
<?php
$str = "<div>text <table>
<tr>
<td valign=top> <IMG src=\"http://test.com/images/test.gif\" title=\"test\">
</tr>
</table> <B>test</B> text <a href=\"test.php\">tester</a> <A HREF=\"index.php?seek=test\" style=\"color:green\">test is tester but is not good</A> text.Test.</div>
<a href='test' >test</a>
<input title='test' />test";
$str.= '<a href="test" >test</test><img src="src" title="test" /><input title=\'test\' />test 1 test 2<b>test1</b>';
$parts = array('test');
foreach ($parts as $part ){
$str = preg_replace('#(<.*/>)?(' . $part . ')(<.*/>)?#m', '$1<span style="font-weight: bold; background-color: red;">$2</span>$3', $str);
}
var_dump( $str );
any ideas !!!
Last edited by
Benjamin on Fri May 15, 2009 8:34 am, edited 1 time in total.
Reason: Changed code type from text to php.
prometheuzz
Forum Regular
Posts: 779 Joined: Fri Apr 04, 2008 5:51 am
Post
by prometheuzz » Fri May 15, 2009 7:30 am
To me, it is not clear what you want. What word "only"? I don't see any substring "only" in your code.
abdz12
Forum Newbie
Posts: 2 Joined: Fri May 15, 2009 3:50 am
Post
by abdz12 » Fri May 15, 2009 10:16 am
sorry i mean by only ( keywords outside html tags ) , and in my code above i assumed "test " as my keyword
prometheuzz
Forum Regular
Posts: 779 Joined: Fri Apr 04, 2008 5:51 am
Post
by prometheuzz » Fri May 15, 2009 10:55 am
abdz12 wrote: sorry i mean by only ( keywords outside html tags ) , and in my code above i assumed "test " as my keyword
Can you provide an actual example including the desired output?
Now all you did is post some example input without telling what you want to get back: what about these strings:
Code: Select all
"abc.test def"
"abc TeSt def"
"abc testing def"
"abc test, def"
...