ThomasJSmart
Programmer
- Sep 16, 2002
- 634
Hi,
Iv been struggeling with this script for like a week -_- its giving me a headache...
what i want to make:
a php function that searches $string for $word and replaces $word for $replace. sounds simple enough right..
thing is... it cant replace $word if $word is not replacable by html code. For example if $word is part of a html link, or if $word is the name of an html tag or if $word is inside some javascript.. .etc.....
so lets say these are the vars:
now 'foo' should only be replaced in 2 of the above instances, the first and the foo between <b> tag.
now i more or less have this kind of working... with a regexp: /(?<!\<)$word(?!\>)/i
but... to make things more complicated... you notice the <div> in the $replace... this div needs to have a unique name in each replace.... barv1 bar2 bar3 etc.... now im seriously stuck.......
the final script should be a function to which the $string is passed. the $word is gotton from a datbase within the function, so several words could be searched for in the $string. im going to pass the word ID to the div name, but then also the replace ID (?) will need to be added to make sure every div has a unique name.
please help
with tips, links, or suggestions ALL VERY welcome and much apreciated!!
Thank you
I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!
Iv been struggeling with this script for like a week -_- its giving me a headache...
what i want to make:
a php function that searches $string for $word and replaces $word for $replace. sounds simple enough right..
thing is... it cant replace $word if $word is not replacable by html code. For example if $word is part of a html link, or if $word is the name of an html tag or if $word is inside some javascript.. .etc.....
so lets say these are the vars:
Code:
$string = 'foo the bar whent over the <a href="foo.html">FOO</a> <b>foo!!</b> <script language="foo>var foo = 123;</script>';
$word = 'foo';
$replace = '<div name="bar1"><a href="foo.html">FOO!</a></div>';
now 'foo' should only be replaced in 2 of the above instances, the first and the foo between <b> tag.
now i more or less have this kind of working... with a regexp: /(?<!\<)$word(?!\>)/i
but... to make things more complicated... you notice the <div> in the $replace... this div needs to have a unique name in each replace.... barv1 bar2 bar3 etc.... now im seriously stuck.......
the final script should be a function to which the $string is passed. the $word is gotton from a datbase within the function, so several words could be searched for in the $string. im going to pass the word ID to the div name, but then also the replace ID (?) will need to be added to make sure every div has a unique name.
please help
Thank you
I learned a bit yesterday, today i learned a lot, imagine what i'll learn tomorrow!