[英]Capture group of number with regex
im trying to capture a group of number behind the " image " word in the url 我试图捕获网址中“ image ”字后面的一组数字
this is the url http://example.com/image123456/super_moon_image_2
这是网址
http://example.com/image123456/super_moon_image_2
and this is my expectations http://my-example.com/get.php?123456
这是我的期望
http://my-example.com/get.php?123456
im using this code its work for some url but the result will broken if there is another number on the url. 即时通讯使用此代码的某些网址的工作,但如果网址上有另一个数字,结果将被破坏。
$link = 'http://example.com/image123456/super_moon_image_2';
$re = '/[^0-9]/';
$str = preg_replace($re, '', $link);
echo 'http://my-example.com/get.php?' . $str;
can someone help me correct the regex thank you. 有人可以帮我纠正正则表达式谢谢。
You can use the following regex pattern: 您可以使用以下正则表达式模式:
(.+?)image(\d+).+
And replace with $1get.php?$2
. 并替换为
$1get.php?$2
。
Here's a regex101 demo . 这是一个regex101演示 。
I don't know much PHP, but your code should look something like this: 我不太了解PHP,但是您的代码应如下所示:
$link = 'http://example.com/image123456/super_moon_image_2';
$re = '/(.+?)image(\d+).+/m';
$subst = '$1get.php?$2';
$newlink = preg_replace($re, $subst, $link);
echo $newlink;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.