2012-07-07 73 views
0

我试图删除这样的事情: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]); 
+1

这样的:的preg_replace( “/ <.*> /”, “你好”,$这个 - > tresc [$ I] [描述]); – 2012-07-07 11:25:11

+0

首先你必须通过附加一个U – 2012-07-07 11:25:56

+0

来制定正则表达式,然后你必须纠正你的正则表达式的尾部,以便识别img标签 – 2012-07-07 11:27:42

回答

-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]); 
-1
$this->tresc[$i][description]=preg_replace("/\<a .*\>.*\<\/a\>/i", "", $this->tresc[$i][description]); 
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)" 
+0

这是干什么的? – Spacedust 2012-07-07 17:46:37

+0

@Spacedust这是一个删除链接的例子。 – 2012-07-07 18:25:15

+0

我以更短的方式做到了。见下文 ;) – Spacedust 2012-07-07 18:38:37