PHP函数不能与String对象一起使用,但可以手动键入它

时间:2010-02-08 10:50:03

标签: php string object function

我正在尝试从来自对象的文本输出中删除标记。问题是,我不能。如果我像"<p>http://www.mylink.com</p>"一样手动输入,它可以正常工作!在执行echo $item->text;时,它会为我提供相同的字符串"<p>http://www.mylink.com</p>";执行var_dump甚至gettype,并为我提供string()。所以,我确定它是一个字符串,但它不是这样的,我尝试了几个函数preg_replacepreg_matchstrip_Tags,没有一个工作。我该如何解决这种情况,如何调试呢?


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

2 个答案:

答案 0 :(得分:0)

尝试以下

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

答案 1 :(得分:0)

在将对象输入函数之前将对象强制转换为字符串。

$ text =(string)$ item-&gt; introtext;