Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/delphi/8.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 正则表达式或函数_Php_Regex - Fatal编程技术网

Php 正则表达式或函数

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内容:

$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);