2012-07-07 74 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