我使用了以下一点代码来查找页面上的所有链接(home.php)并将它们作为URL回显。它工作正常,但如何将结果传递给新变量?如果我创建一个新变量:
$myvariable ="$element->href";
这只是许多人的最后结果。
// Create DOM from URL or file
$html = file_get_html('http://www.somewebsite.xxx/include/home.php');
foreach($html->find('a') as $element)
echo $element->href . '<br>';
答案 0 :(得分:0)
使用array:
// Create DOM from URL or file
$html = file_get_html('http://www.somewebsite.xxx/include/home.php');
$urls = array();
foreach($html->find('a') as $element) {
$urls[] = $element->href;
}
print_r($urls);
答案 1 :(得分:0)
$myvar = '';
foreach($html->find('a') as $element) {
$myvar .= $element->href . '<br>';
}
或使用Array:
foreach($html->find('a') as $element) {
$myvar[] = $element->href; // removed <br> for implode, you can add it back
}
// if you want the array as one string
$myvar = implode('<br>', $myvar);
答案 2 :(得分:0)
您可以使用数组来保存该问题页面中所有链接的值。在End中,Array是您要查找的变量。方法如下:
<?php
//USE THE HTML DOM PARSER TO PARSE ALL THE HTML DATA ON THE PAGE: $page
$page = 'http://www.somewebsite.xxx/include/home.php';
$html = file_get_html($page);
// LOOPING THROUGH THE DOM ELEMENTS SELECT ONLY THE <a> TAGS
// AND BUNDLE THEM INTO AN ARRAY...
// THE ARRAY NOW FORMS THE VARIABLE YOU HAD EXPECTED TO CREATE..
$arrAnchors = array(); // INITIALIZE $arrAnchors TO AN EMPTY ARRAY...
foreach($html->find('a') as $element) {
// PUSH ALL THE ANCHOR'S HREF ATTRIBUTES (URLs) INTO THE $arrAnchors ARRAY
$arrAnchors[] = $element->href . '<br>';
}
// NOW TRY TO DUMP THE CONTENT OF YOUR $arrAnchors....
var_dump($arrAnchors); // DISPLAYS A NUMERICALLY INDEXED ARRAY OF LINKS ON THE PAGE: $page