XML parsing: Uncaught TypeError: Object [object Object] has no method 'suggest'

三世轮回 提交于 2019-12-12 06:28:14

问题


$results = simplexml_load_string($results);
foreach($results as $result){
    $result_title = $result->label;
    $result_title = preg_replace('#[^a-zA-Z]+Extra Title#', '', $result_title);
    $result_title = ltrim($result_title);

The first preg_replace gets rid of a pattern at the end of the $result->label (which var_dumps as an XML object) and outputs, not an XML object, but a string.

This is surprising because I thought that preg_replace would only work on strings and not the XML object. A var_dump of the $result reveals:

object(SimpleXMLElement)#1003 (1) { [0]=> string(31) " Some Text > Extra Title" }

Note the extra white space before 'Some Text'. The second ltrim($result_title);, however, causes this error:

Uncaught TypeError: Object [object Object] has no method 'suggest'

So what am I dealing with? A string or an object? And how do I trim the whitespace?

Trying a preg_replace for the first whitespace also outputs the same error as above.

$result_title = preg_replace('/\s/', '', $result_title, 1);

来源:https://stackoverflow.com/questions/12521634/xml-parsing-uncaught-typeerror-object-object-object-has-no-method-suggest

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!