【问题标题】:PHP functions wont work with String object, but works with it typed manuallyPHP 函数不能与 String 对象一起使用,但可以与手动键入的对象一起使用
【发布时间】:2010-02-08 10:50:02
【问题描述】:

我正在尝试从来自对象的文本输出中去除标签。问题是,我做不到。如果我像"<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;

【问题讨论】:

    标签: php string object function


    【解决方案1】:

    试试下面的

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

    【讨论】:

      【解决方案2】:

      在将对象输入函数之前将其类型转换为字符串。

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

      【讨论】:

        猜你喜欢
        • 2021-12-08
        • 2011-04-25
        • 2015-08-06
        • 1970-01-01
        • 2022-01-04
        • 2020-06-26
        • 1970-01-01
        • 2012-03-02
        • 2014-03-20
        相关资源
        最近更新 更多