Welcome Guest, Not a member yet? Register   Sign In
preg_replace function problem
#1

[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";
$string = "WEB DESIGN Web Design web design <a href='google.com'>Web design las vegas</a>";
$finalstring = preg_replace('/('.$find.'\b)/iU', '<a href="http://google.com">\1</a>', $string,-1,$i);
echo $finalstring;
echo '<br />'.$i;
#2

[eluser]theprodigy[/eluser]
Quote: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
Please specify what you mean by "how to exclude the one with link from being replaced ?".
I do not understand what you are asking for.
#3

[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
#4

[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
#5

[eluser]theprodigy[/eluser]
Try this and see how close you get:
Code:
$find = "[^<a.*?&gt;].*?(web design)+?.*?[^</a>]";
$string = "WEB DESIGN Web Design web design <a href='google.com'>Web design las vegas</a>";
$finalstring = preg_replace('/('.$find.'\b)/iU', '<a href="http://google.com">\1</a>', $string,-1,$i);
echo $finalstring;
echo '<br />'.$i;
#6

[eluser]basty_dread[/eluser]
hi
i try to modify your code this way:
Code:
$find = "web design";
    $string = "WEB DESIGN Web Design web design <a href='yahoo.com'>web design google</a> las vegas";
$finalstring = preg_replace('/([<a[^>]*>].*?('.$find.')+?.*?[^<\/a>]\b)/iU', '<a href="http://google.com">\1</a>', $string,-1,$i);

echo $finalstring;
echo '<br />'.$i;
it exclude the one with link but the other word that should be replaced with a new link was left untouched..
my aim is that it it will leave the word with link and put a link on the other word..
#7

[eluser]theprodigy[/eluser]
Quote:i try to modify your code this way:
Code:
$finalstring = preg_replace('/([<a[^>]*>].*?('.$find.')+?.*?[^<\/a>]\b)/iU', '<a href="http://google.com">\1</a>', $string,-1,$i);
Quote:Try this and see how close you get:
Code:
$find = "[^<a.*?&gt;].*?(web design)+?.*?[^</a>]";
I understand what you tried to do, but your regex looks a bit different.
Try this one:
Code:
$finalstring = preg_replace('/[^<a.*?&gt;].*?('.$find.')+?.*?[^</a>]\b)/iU', '<a href="http://google.com">\1</a>', $string,-1,$i);
#8

[eluser]basty_dread[/eluser]
it says Unknown modifier 'a'
#9

[eluser]theprodigy[/eluser]
not as a fix for the regex, but your replacement string should be:
Code:
<a href="http://google.com">\\1</a>
from PHP's website:
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.
#10

[eluser]theprodigy[/eluser]
Code:
$finalstring = preg_replace('/(?!<a\b[^>]*>).*?('.$find.').*?(?!</a>)\b)/iU', '<a href="http://google.com">\\1</a>', $string,-1,$i);
Try that.




Theme © iAndrew 2016 - Forum software by © MyBB