2016-03-02 68 views
2

我正在使用简单的HTML DOM解析器库来解析带有DOCTYPE标记的URL。我能够达到所需的标签,但每当我回应或尝试存储结果时,它都不会显示任何内容。但是,var_dump()显示我需要的字符串。Var_dump()显示String,echo()/ print_r()返回Nothing

require_once("simpledom.php"); 

$url="http://google.com"; 
$sPage = file_get_contents($url); 
$sPageContent = new simple_html_dom(); 
$sPageContent->load($sPage); 
$sObjects = $sPageContent->find('unknown'); 

foreach($sObjects as $sKey) 
{ 
    var_dump($sKey->_[4]); /*this var_dump shows the stuff */ 
    $showres = $sKey->_[4] 
} 

/* this variable should hold the string but it shows nothing */ 
echo $showres; 
+1

什么类型的var_dump显示你? – PeterPan666

+0

var_dump显示了字符串'<!doctype html>'(length = 15) –

+0

您是否试图在循环内回显,就像您在做var_dump一样? –

回答

3

当输出<!doctype html>时,浏览器读取该元素,因此您需要对<>符号进行编码。 PHP有一个已经建立的功能,http://php.net/manual/en/function.htmlspecialchars.php

所以,你的代码应该是:

echo htmlspecialchars($showres); 

这在源会给你

&lt;!doctype html&gt; 
-2

尝试值铸造成一个字符串

$showres = (string)$sKey->_[4]; 

此外,你的回音是你的循环后

+1

这是一个已经OP说过的字符串; 'var_dump显示我字符串'<!doctype html>'(length = 15)'。 – chris85

+1

该值已经是一个字符串,请参阅第二条评论。 – PeterPan666

+0

对不起@David,它没有帮助 –

1
echo htmlentities($showres); 

回音必什么在$showres和替换HTML实体,因此所有的HTML标记您可以看到该字符串,并且不会让浏览器将其用作标记。

不知道你想要做什么。