使用preg_replace删除不需要的链接

时间:2012-07-07 11:19:14

标签: php parsing preg-replace

我正在尝试删除这样的内容:

<a href="http://women.domain.com">women.domain.com</a>

我认为应该是:

$this->tresc[$i][description]=preg_replace("/\<a(.*)href=(\"|')http:\/\/women\.domain\.com.*(\"|')(.*)\/\>/i", "",$this->tresc[$i][description]);

但它不起作用。

对于图像我使用它并且它完美地工作:

$this->tresc[$i][description]=preg_replace("/\<img(.*)src=(\"|')http:\/\/women\.domain\.com.*(\"|')(.*)\/\>/i", "",$this->tresc[$i][description]);

3 个答案:

答案 0 :(得分:0)

$pattern = '(\<a href=["|\']+[http(s)?:\/\/]+([a-z0-9\-]+[.]){1,}+[a-z]{2,4}+[\/]+(.*)+["|\']\>+(.*)+\</a\>)';
$string = "click <a href='http://subdomain.sitename.tld/somedir'>here</a>";

var_dump(preg_replace($pattern, "", $string)); // prints "string 'click ' (length=6)"

答案 1 :(得分:-1)

$this->tresc[$i][description]=preg_replace("/\<a .*\>.*\<\/a\>/i", "", $this->tresc[$i][description]);

答案 2 :(得分:-1)

$this->tresc[$i][description]=preg_replace("/<a href=\"(.*)\">/i", "",$this->tresc[$i][description]);
$this->tresc[$i][description]=preg_replace("/<\/a\>/i", "",$this->tresc[$i][description]);