Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/250.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/19.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链接_Php_Regex_String - Fatal编程技术网

表达式从PHP中的文本中删除最后一个URL链接

表达式从PHP中的文本中删除最后一个URL链接,php,regex,string,Php,Regex,String,我想删除字符串中最后出现的url 输入 祝你一切顺利 输出 祝你一切顺利 输入 谢谢你让我接触了300万。这不是我的成就。。。这是你的爱。永远感激 输出 谢谢你让我接触了300万。这不是我的成就。。。这是你的爱。永远感激 这样做可以: https?://\S+(?!.*https?://\S+) 您可以尝试以下代码 $str = "Thank you for making me touch 3 Million. https://google.com/first. It’s not MY ach

我想删除字符串中最后出现的url

输入

祝你一切顺利

输出

祝你一切顺利

输入

谢谢你让我接触了300万。这不是我的成就。。。这是你的爱。永远感激

输出

谢谢你让我接触了300万。这不是我的成就。。。这是你的爱。永远感激


这样做可以:

https?://\S+(?!.*https?://\S+)

您可以尝试以下代码

$str = "Thank you for making me touch 3 Million. https://google.com/first. It’s not MY achievement... it’s your LOVE. https://google.com/second. Gratitude forever https://google.com/last";
//$str = "Hey All the Best. https://google.com/last";
$p = "/(https?:\/\/[^\/]+(?:[\d\w\/\._]*)\s*)$/i";
$result = preg_replace($p, '', $str);
var_dump($result);

正则表达式中的“$”是最后一个匹配项,如果未添加“$”,则所有链接都将匹配。与此相对应的是“^”,与开头匹配。

虽然这可能会回答问题,但您应该在回答中包含一些关于此操作原理的解释。代码块本身并不会立即对以后可能遇到相同问题的人有用。谢谢您的提醒@霍普德普皮努特酒店