How to use backreferences in PHP
Posted
by
Slinky
on Stack Overflow
See other posts from Stack Overflow
or by Slinky
Published on 2011-01-14T17:47:51Z
Indexed on
2011/01/14
17:53 UTC
Read the original article
Hit count: 241
I want to add a character to the end of each file extension found in a body of text using preg_replace().
Here is some sample text:
$string='http://www.mysite.com/expert/images/imageone.jpghttp://www.mysite.com/expert/images/imagetwo.jpg';
This search & replace works fine in TextWrangler, appending a semi colon to file extensions:
(\.(jpg|gif|html?|php|tiff?|pdf|png))
\1;
Translated to PHP, however does not work, having no effect; no errors.
preg_replace("/(\.(jpg|gif|html|php|tif|tiff|pdf|htm|png))/","\\1;",$string);
© Stack Overflow or respective owner