Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.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
使用preg_replace在php中保留字符串的一部分_Php_Regex_Preg Replace - Fatal编程技术网

使用preg_replace在php中保留字符串的一部分

使用preg_replace在php中保留字符串的一部分,php,regex,preg-replace,Php,Regex,Preg Replace,我有下面的字符串 http://www.sample.com/profile/272353.html 我想用preg_replace来保留这部分字符串 profile/272353.html 字符串的第一部分发生了变化,所以我只想确保保留strng的这一部分 我不熟悉模式,但我仍然没有得到它,因此希望您能提供任何帮助。使用parse\u url(…) 好吧,回答我所问的问题 $modified = preg_replace("/.*([^\/]+\/[^\/]+$)/", $1, $stri

我有下面的字符串

http://www.sample.com/profile/272353.html
我想用preg_replace来保留这部分字符串

profile/272353.html
字符串的第一部分发生了变化,所以我只想确保保留strng的这一部分

我不熟悉模式,但我仍然没有得到它,因此希望您能提供任何帮助。

使用parse\u url(…)


好吧,回答我所问的问题

$modified = preg_replace("/.*([^\/]+\/[^\/]+$)/", $1, $string);

preg\u replace()
在哪里适合所有这些?有很多方法可以做到这一点。@DavidHarris在下面说,使用
parse\u url
。正则表达式不是一根魔杖,你不应该在每一个与字符串有关的编程问题上挥舞它。就像我说的,我是新来的,所以我尝试以任何更容易的方式来处理这个问题。谢谢你的函数来分解url,但是如果不是一个格式良好的url,或者只是url的一部分,我实际上先尝试一下,我得到的字符串有时不是url。我考虑使用preg_replace-its的唯一原因是因为我有一个函数,我使用preg_replace函数从字符串中保留数字。我不知道我做的是否正确,但它是有效的。但是,任何关于如何实现的想法都将受到赞赏。
$modified = preg_replace("/.*([^\/]+\/[^\/]+$)/", $1, $string);