Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中,替换URL中的7位或更多数字,但不替换URL末尾的7位或更多数字_Php_Regex - Fatal编程技术网

在PHP中,替换URL中的7位或更多数字,但不替换URL末尾的7位或更多数字

在PHP中,替换URL中的7位或更多数字,但不替换URL末尾的7位或更多数字,php,regex,Php,Regex,我想替换URL中间的7位或更多数字模式。模式必须只有像/12345678这样的数字,而不是像/1234567ab\u et这样的数字,如果模式是这样的/123456789.jpg 我对正则表达式很陌生。我尝试过使用这个表达式\/(\d{7,})[^.\d],但它的结尾需要一个额外的字符 示例url:/uploads/2016/04/1512101244aaaaaafasdfasdfdas/12345678/a1972_porsche911S24;arga-0-1024/234567889.jpg

我想替换URL中间的7位或更多数字模式。模式必须只有像
/12345678
这样的数字,而不是像
/1234567ab\u et
这样的数字,如果模式是这样的
/123456789.jpg

我对正则表达式很陌生。我尝试过使用这个表达式
\/(\d{7,})[^.\d]
,但它的结尾需要一个额外的字符

示例url:
/uploads/2016/04/1512101244aaaaaafasdfasdfdas/12345678/a1972_porsche911S24;arga-0-1024/234567889.jpg
从这个url我只想替换这个
/12345678


预期url:
/uploads/2016/04/1512101244aaaaaafasdfasdfdas/a1972\u Porsche\u 911S24Targa-0-1024/234567889.jpg

您好,您应该删除
[^.\d]
并添加
\/
,最终正则表达式类似
\/(\d{7,})\//code>

此正则表达式的图形表示形式如下所示:

它与您提供的url配合使用:


从模式中删除
[^.\d]
。@WiktorStribiż我也尝试过
[^.\d]
。它需要所有超过7位的数字,例如:(regexr.com/5992l)太棒了!!!从你的回答中,我已经知道了如何匹配最终的正则表达式。我将用这个
\/?(\d{7,})\/
谢谢@NamyshWhy你在使用一个捕获组吗?这里没用。@托托你是对的,这里没用,但我把它放在这里,以防他想要检索匹配的id值