Php 如何在没有img src的情况下删除http和https
代码:Php 如何在没有img src的情况下删除http和https,php,regex,Php,Regex,代码: $str='1〕http://www.google.com '; $str=preg\u replace('/\b(https?):\/\/[-A-Z0-9+&-\/%?=~~~~~~$!:,.;]*[A-Z0-9+&~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~; echo$str; 输出: $str = 'http://www.google.com <i
$str='1〕http://www.google.com ';
$str=preg\u replace('/\b(https?):\/\/[-A-Z0-9+&-\/%?=~~~~~~$!:,.;]*[A-Z0-9+&~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~;
echo$str;
输出:
$str = 'http://www.google.com <img src="http://placehold.it/350x150" />';
$str = preg_replace('/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i', '', $str);
echo $str;
我需要这个输出:
<img src="" />
我怎么做
谢谢你的帮助。你的模式
<img src="http://placehold.it/350x150" />
删除以协议http
或https
开头的所有URL(字符串中)。因此,当您将其应用于字符串时,它将同时删除字符串开头的URL和的src
URL;
$str=preg\u replace('/^\b(https?:\/\/[-A-Z0-9+&@#\/%?=~~~~|$!:,.;]*[A-Z0-9+&~~|$]/i',''.$str);
echo$str;
或者只需像这样获得您需要的:
/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i
/()/i
你的模式
<img src="http://placehold.it/350x150" />
删除以协议http
或https
开头的所有URL(字符串中)。因此,当您将其应用于字符串时,它将同时删除字符串开头的URL和的src
URL;
$str=preg\u replace('/^\b(https?:\/\/[-A-Z0-9+&@#\/%?=~~~~|$!:,.;]*[A-Z0-9+&~~|$]/i',''.$str);
echo$str;
或者只需像这样获得您需要的:
/\b(https?):\/\/[-A-Z0-9+&@#\/%?=~_|$!:,.;]*[A-Z0-9+&@#\/%=~_|$]/i
/()/i
我还认为
DOMDocument
和DOMXPath
是解析HTML标记的首选工具但就您的特殊情况而言,下面是使用regexp负查找断言的解决方案:
$str='1〕http://www.google.com http://www.google.com.ua';
$str=preg_replace('/(?我还认为DOMDocument
和DOMXPath
是解析HTML标记的首选工具。
但就您的特殊情况而言,以下是使用regexp反向查找断言的解决方案:
$str='1〕http://www.google.com http://www.google.com.ua';
$str=preg_replace('/(?Try:
]*(*SKIP)(*FAIL)|\b(https?):\/\/[-A-Z0-9+&-\/%?=~~+$!:,.;]*[A-Z0-9+&-\/%=~~\$]
]*
捕捉未关闭的中的所有内容,请尝试:
]*(*SKIP)(*FAIL)|\b(https?):\/\/[-A-Z0-9+&-\/%?=~~+$!:,.;]*[A-Z0-9+&-\/%=~~\$]
]*
捕获未关闭的中的所有内容。使用DOMDocument和DOMXPath查找所有文本节点并对其应用您的模式。尝试。因此$1
包含您需要的内容。使用DOMDocument和DOMXPath查找所有文本节点并对其应用您的模式。尝试。因此$1
包含您需要的内容。
$str = 'http://www.google.com <img src="http://placehold.it/350x150" /> http://www.google.com.ua';
$str = preg_replace('/(?<!src=\")(https|http):\/\/[^\s]+\b/i', '', $str);
print_r($str); // <img src="http://placehold.it/350x150" />