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-17-2010

[eluser]basty_dread[/eluser]
still it doesnt work ...
it removes everythin..
can this be done?
exact match only if it find that web design google has a >
ie.
<a href='link'>web[b] design google </a> las vegas
it will not replace this because there is > beside web


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

[eluser]theprodigy[/eluser]
Code:
$finalstring = preg_replace('/(^|[^<a.*?&gt;])('.$find.').*?[^</a>]*?/iU', '<a href="http://google.com">\\2</a>', $string,-1,$i);
I believe that should do it. It took me a while, but I think I finally got it (keeping fingers crossed).


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

[eluser]basty_dread[/eluser]
do you have a testing server like xampp?

i tried to modify it again just removing some values and it turned good, but it removes the spacing between then

can you try this?
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.*?&gt;])('.$find.')\b/iU', '<a href="http://google.com">\2</a>', $string,-1,$i);
echo $finalstring;
echo '<br />'.$i;



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

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



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

[eluser]basty_dread[/eluser]
works great.. thanks a lot theprodigy..
it takes me so much time try to figure it out..

i was working on crosslinking within the site..

things like this also could occur
Code:
$find = "designing";
$string = "<a href='link'>web desiging for all</a> nice web design";
$finalstring = preg_replace('/(^|[^<a.*?&gt;])('.$find.')\b/iU', '\1<a href="http://yahoo.com">\2</a>', $string,-1,$i);

we will surely have an error with this..

i appreciate your help.. thanks a lot for helping me on it. Many Thanks