Php 正则表达式或函数
我有如下html内容:Php 正则表达式或函数,php,regex,Php,Regex,我有如下html内容: $html = "My name is Varun-Kumar. My webpage is <a href='http://varundeboss.com/varun-home-page'>Varundeboss</a> Also http://varundeboss.home.com/varun-home-page"; $html=“我的名字是瓦伦·库马尔。我的网页也是http://varundeboss.home.com/varun-hom
$html = "My name is Varun-Kumar. My webpage is <a href='http://varundeboss.com/varun-home-page'>Varundeboss</a> Also http://varundeboss.home.com/varun-home-page";
$html=“我的名字是瓦伦·库马尔。我的网页也是http://varundeboss.home.com/varun-home-page";
现在,我想从html中删除所有出现的“-”,除非它出现在锚标记中,以及以“http://”、“https://”和“www”开头的链接中
我可以使用以下代码为锚标记执行此操作:
$result = preg_replace('%-(?![^<]*</a>)%i', '', $html);
$result=preg_replace('%-(?![^您可以使用以下模式:
$result = preg_replace('~(?:https?:\S+|<a\b[^>]*)(*SKIP)(?!)|-~i', ' ', $html);
在这两个示例中,您可以轻松添加所需的内容:img标记、www等
$result = preg_replace_callback('~(https?:\S+|<a\b[^>]*)|-~i',
function ($m) { return ($m[1])? $m[1] : ' ';},
$html);