2010-02-08 182 views
0

我試圖從來自對象的文本輸出中去除標籤。問題是,我不能。如果我像"<p>http://www.mylink.com</p>"那樣手動鍵入它,它工作正常!當做echo $item->text;它給了我相同的字符串"<p>http://www.mylink.com</p>";var_dump或甚至gettype,給我一個string()。所以,我確定它是一個字符串,但它不是這樣,我嘗試了幾個函數preg_replacepreg_matchstrip_Tags,都沒有工作。我該如何解決這種情況,如何進行調試?PHP函數不適用於字符串對象,但適用於手動輸入


$search = array("<p>", "</p>"); 
$switch = array("foo", "baa"); 

//works just fine, when used 
$text = "<p>http://www.mylink.com</p>"; 

//it's a string for sure! 
var_dump($item->introtext); 

$text = $item->introtext; 

//doesn't work 
$text = str_replace($search, $switch, $text); 

$text = strip_tags($text, "<p>"); 

//doesn't work either. 
$matches = array(); 
$pattern = '/<p>(.*)<\/p>/'; 

preg_match($pattern, $text, $matches); 

//gives me the following output: <p>http://www.omeulink.com</p> 
echo $text; 

回答

0

請嘗試以下

$text = $item->introtext; 
$newText = strip_tags($text); 
+0

hmm也可以工作 – Deefjuh 2010-02-08 11:02:46

0

強制轉換對象爲字符串你給它進入函數之前。

$ text =(string)$ item-> introtext;

相關問題