CodeIgniter Forums
preg_replace function problem - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21)
+--- Thread: preg_replace function problem (/showthread.php?tid=27636)

Pages: 1 2


preg_replace function problem - El Forum - 02-16-2010

[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;



preg_replace function problem - El Forum - 02-16-2010

[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.


preg_replace function problem - El Forum - 02-16-2010

[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



preg_replace function problem - El Forum - 02-16-2010

[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


preg_replace function problem - El Forum - 02-16-2010

[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;



preg_replace function problem - El Forum - 02-16-2010

[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..


preg_replace function problem - El Forum - 02-16-2010

[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);



preg_replace function problem - El Forum - 02-16-2010

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


preg_replace function problem - El Forum - 02-16-2010

[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.


preg_replace function problem - El Forum - 02-16-2010

[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.