我正在使用Simple HTML Dom,尝试从网站获取字符串。当我在函数中打印$ title [0]时它只显示一个字符串,但是当我在返回数组中保护它并打印出返回值时,我会收到一个带有 RECURSION 的永无止境的文本。 我不明白为什么它适用于第二个变量$ oTitle。
<?php
include 'scripts/simple_html_dom.php';
function getDetails($id) {
$url = "http://www.something.com";
$html = file_get_html ( $url );
$title = $html->find('span[itemprop=name]');
print_r($title[0] . PHP_EOL); //prints out the correct title
$oTitle = "Something"; //there is also code for this variable but it works as it should
$details = array("Title" => $title[0], "Original Title" => $oTitle);
return $details;
flush ();
}
$values = getDetails($number);
print_r($values); //code breakes here
?>
答案 0 :(得分:1)
请看这个页面:http://simplehtmldom.sourceforge.net/
我可以看到,您正在使用此解析器。
为了获取HTML内容,您应该使用以下内容:
// Create DOM from URL or file
$html = file_get_html('http://www.google.com/');
// Find all images
foreach($html->find('img') as $element)
echo $element->src . '<br>';
// Find all links
foreach($html->find('a') as $element)
echo $element->href . '<br>';
要删除内容,您应该使用以下内容:
// Dump contents (without tags) from HTML
echo file_get_html('http://www.google.com/')->plaintext;
试试这段代码:
<?php
include 'simple_html_dom.php';
function getDetails() {
$url = "http://www.godaddy.com";
$html = file_get_html ( $url );
$title = getTitle($url);
echo $title; //prints out the correct title
$oTitle = "Something"; //there is also code for this variable but it works as it should
$details = array("Title" => $title, "Original Title" => $oTitle);
return $details;
flush ();
}
function getTitle($Url){
$str = file_get_contents($Url);
if(strlen($str)>0){
preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
return $title[1];
}
}
$values = getDetails();
print_r($values); //code breakes here
?>