PHP预匹配url
我对PHP中的preg_match有一个问题 我有网址:PHP预匹配url,php,regex,preg-replace,Php,Regex,Preg Replace,我对PHP中的preg_match有一个问题 我有网址: http://mysite.com/file/w867/2612512232 http://mysite.com/file/c3233/2123255464 etc. 我需要网址: http://mysite.com/file/2612512232 http://mysite.com/file/2123255464 我必须删除: w867/ c3233/ etc. 您不一定需要使用preg_match。parse_url()可以完成
http://mysite.com/file/w867/2612512232
http://mysite.com/file/c3233/2123255464
etc.
我需要网址:
http://mysite.com/file/2612512232
http://mysite.com/file/2123255464
我必须删除:
w867/
c3233/
etc.
您不一定需要使用preg_match。parse_url()可以完成这项工作
只需确保在没有您不想要的部分的情况下将其全部连接起来。您可以尝试以下模式:
“/(.*)\/([^/])*\/(.*)/”
然后使用str_replace,您可以:$string=str_replace('/'.$matches[2],''$string)代码>
这将在“文件/零件”后面的“/”符号之间搜索第一个图案
preg_replace("|file/\w+/|", "file/", $url);