开发者

Preg Replace Question PHP

I have a url, http://www.jdocy.com/click-42343-32422

I want to replace the 42343 开发者_C百科section of the url with php's preg_replace function.

How would I go about doing this?


Do you want to transform www.jdocy.com/42343-32422 to www.jdocy.com/32422? If so, you would use backreferences, something like:

 $url = preg_replace('/^(.+/)[0-9]+-([0-9]+)$/', '$1$2', $url)


$rep = '\1' + addslashes(111111) + '-\3';
$url = preg_replace('#(/)([\d]+)-([\d]+)#', $rep, $url);

Where 111111 is what you want to add. Addslashes is necessary to prevent accidentally including a backreference (\1, \2, \3)...

Edit: Fix missing quote


$url = preg_replace('`(?<=\.com/click-)\d+`i', 'replacement text', $url);

Use a look-behind and you don't need to re-insert any text.


$url = preg_replace('/^(.+/)[0-9]+-([0-9]+)$/', '$1$2', $url)

[spam edited out]

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜