开发者

How to use backreferences in PHP

开发者 https://www.devze.com 2023-02-04 14:57 出处:网络
I want to add a character to the end of each file extension found in a body of text usi开发者_如何学编程ng preg_replace().

I want to add a character to the end of each file extension found in a body of text usi开发者_如何学编程ng 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);


It works perfectly for me, but you should try using $1:

$string = preg_replace("/.../","$1;",$string);

or put the replacement in single quotes:

$string = preg_replace("/.../",'\\1;',$string);
0

精彩评论

暂无评论...
验证码 换一张
取 消