preg_replace function problem |
[eluser]basty_dread[/eluser]
hi guys. please help me with this problem how to exclude the one with link from being replaced ? this is my problem with my pattern, how i will exclude the one with link... Thanks Code: $find = "web design";
[eluser]theprodigy[/eluser]
Quote:hi guys. please help me with this problemPlease specify what you mean by "how to exclude the one with link from being replaced ?". I do not understand what you are asking for.
[eluser]basty_dread[/eluser]
the one with link is Code: <a href='yahoo.com'>Web design las vegas</a> this is where we will search: Code: $string = "WEB DESIGN Web Design web design <a href='yahoo.com'>web design google</a> las vegas"; i used this preg_replace function, but it replaced web design google las vegas turning the result into: Code: <a href='yahoo.com'><a href="http://google.com">web design</a> google</a> las vegas
[eluser]basty_dread[/eluser]
i dont know how could i exclude it from being replaced. because it is already a link so it should skip the word that has already a link. thanks for helping
[eluser]theprodigy[/eluser]
Try this and see how close you get: Code: $find = "[^<a.*?>].*?(web design)+?.*?[^</a>]";
[eluser]basty_dread[/eluser]
hi i try to modify your code this way: Code: $find = "web design"; my aim is that it it will leave the word with link and put a link on the other word..
[eluser]theprodigy[/eluser]
Quote:i try to modify your code this way: Quote:Try this and see how close you get:I understand what you tried to do, but your regex looks a bit different. Try this one: Code: $finalstring = preg_replace('/[^<a.*?>].*?('.$find.')+?.*?[^</a>]\b)/iU', '<a href="http://google.com">\1</a>', $string,-1,$i);
[eluser]theprodigy[/eluser]
not as a fix for the regex, but your replacement string should be: Code: <a href="http://google.com">\\1</a> Quote:replacement may contain references of the form \\n or (since PHP 4.0.4) $n, with the latter form being the preferred one. Every such reference will be replaced by the text captured by the n'th parenthesized pattern. n can be from 0 to 99, and \\0 or $0 refers to the text matched by the whole pattern. Opening parentheses are counted from left to right (starting from 1) to obtain the number of the capturing subpattern. To use backslash in replacement, it must be doubled ("\\\\" PHP string).I'll continue trying to figure out the regex.
[eluser]theprodigy[/eluser]
Code: $finalstring = preg_replace('/(?!<a\b[^>]*>).*?('.$find.').*?(?!</a>)\b)/iU', '<a href="http://google.com">\\1</a>', $string,-1,$i); |
Welcome Guest, Not a member yet? Register Sign In |