【问题标题】:Simple HTML DOM Parser in input field输入字段中的简单 HTML DOM 解析器
【发布时间】:2020-10-01 01:34:44
【问题描述】:

我正在尝试从 DOM 解析器中获取所有结果,但我只获取了输入字段中的最后一个标签。如果我回显 $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.com/inbazhlekov/domm

我希望输入字段中的值为:“Football Espagne LaLiga”,而不仅仅是“LaLiga”。

【问题讨论】:

  • $link 是循环中的最后一个文本。将 $link->plaintext 赋值给像 $inputValue 这样的变量。所以你的代码应该是这样的$inputValue .= $link-&gt;plaintext;。并且不要忘记更改输入值。

标签: php html parsing dom simple-html-dom


【解决方案1】:

循环将在各自循环的每次迭代中覆盖$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;
    }
}
?>

当你要输出时,implode 以空格为分隔符的数组:

<input type="text" name="name" value="<?= implode(' ', $name)  ?>"><br>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 2016-07-30
    • 2015-05-17
    • 2015-02-14
    • 1970-01-01
    相关资源
    最近更新 更多