我想从DOM解析器中得到所有的结果,但我只得到输入字段中的最后一个标签。如果我echo $link不是问题。有什么办法可以像我从echo得到的那样得到所有的文本吗?
<?php
include_once('simple_html_dom.php');
// Create DOM from URL or file
$link = "https://www.sofascore.com/fr/sevilla-real-betis/qgbsIgb";
foreach ($html->find('div.Header__Main-sc-1dyxed1-0.hkVBhG ul li') as $element){
$links = $element->find('a');
foreach ($links as $link) {
echo $link->plaintext.'<br/>';
}
}
?>
<input type="text" name="name" value="<?php echo $link->plaintext?>"><br>
这里是github。https:/github.cominbazhlekovdomm。
我希望输入字段中的值是:"Football Espagne LaLiga",而不仅仅是 "LaLiga"。"Football Espagne LaLiga", 而不是只有 "LaLiga".
循环会覆盖 $element
和 $link
在各自循环的每次迭代中。如果你想在循环后访问所有的值,你需要将这些值存储在一个变量中。
你可以将它们存储在一个数组中。
$link = "https://www.sofascore.com/fr/sevilla-real-betis/qgbsIgb";
// Initiate the array we'll use to store the values
$name = [];
foreach ($html->find('div.Header__Main-sc-1dyxed1-0.hkVBhG ul li') as $element){
$links = $element->find('a');
foreach ($links as $link) {
// Push the values into our array instead of echoing them
$name[] = $link->plaintext;
}
}
?>
当你想输出的时候, 内爆 用空格作为定界符的数组。
<input type="text" name="name" value="<?= implode(' ', $name) ?>"><br>