tags:

views:

63

answers:

2

Hello all. I have a little problem with my regular expression, that I use in PHP. My code identify all tags of my content and add a link in this image. My code is working when I use dinamycally, without any defined image. When I try with a imapge path, the code does not work. How can I solve this problem?

Working code:
$content = preg_replace('/(<img .*?src="(.+?)".*?>)/','<a class="nyromodal foto" href="'.$imagem_wordpress.'">\1</a>', $content);


Problem code:
$content = preg_replace('/(<img .*?src="http://mysite.com/files/2010/04/bac-gallery-site-matters-saline-project1.jpg".*?&gt;)/','&lt;a class="nyromodal foto" href="'.$imagem_wordpress.'">\1</a>', $content); 
+2  A: 
$content = preg_replace('/(<img .*?src="http:\/\/mysite.com\/files\/2010\/04\/bac-gallery-site-matters-saline-project1\.jpg".*?>)/','<a class="nyromodal foto" href="'.$imagem_wordpress.'">\1</a>', $content);

You forgot to escape your forward slashes. (And as pointed out by others, it should be "http" and not "ttp"

webdestroya
You forgot to escape the first `.` in the url. Although the regex still matches, it'll also match `mysitescom/files/...`, which probably isn't what you want
Eric
Really thanks webdestroya, works very well.
Marcos
Choosing a delimiter other than '/' will result in a less cluttered regex: `#(<img .*?src="http://mysite.com/files/2010/04/bac-gallery-site-matters-saline-project1\.jpg".*?>)#`
Bart Kiers
A: 

Should that be src="http in the problem code instead of src="ttp?

rayners
Yes, is http...
Marcos