Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/88.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用简单的HTMLDOM解析列表_Php_Html_Parsing_Html Lists_Simple Html Dom - Fatal编程技术网

Php 如何使用简单的HTMLDOM解析列表

Php 如何使用简单的HTMLDOM解析列表,php,html,parsing,html-lists,simple-html-dom,Php,Html,Parsing,Html Lists,Simple Html Dom,我有一个html代码,并且我面临着一个问题,就是从下面给出的部分解析来自此html的数据: <li id=xyz> John Johnson <sup>1<sup> "," </li> 约翰·约翰逊 1. "," 我只想把“约翰·约翰逊”从这个名单中抽出来,别的什么都不想。不知道怎么做。谢谢。请将您需要的内容用类似的方式围绕起来。 与之相比,这是一个不错的选择 <li id='xyz'> <span>John

我有一个html代码,并且我面临着一个问题,就是从下面给出的部分解析来自此html的数据:

<li id=xyz>
  John Johnson
<sup>1<sup>
","
</li>
  • 约翰·约翰逊 1. ","

  • 我只想把“约翰·约翰逊”从这个名单中抽出来,别的什么都不想。不知道怎么做。谢谢。

    请将您需要的内容用类似的方式围绕起来。 与之相比,这是一个不错的选择
    <li id='xyz'>
      <span>John Johnson</span>
      <sup>1<sup>
        ","
    </li>
    

    使用简单的javascript,您可以为span提供一个类或Id,并从javascript获取它

    <span id="grabIt">John Johnson</span>
    
    find('text')
    是您所追求的。它返回源中找到的所有文本块

    根据您的示例,这里有一个工作代码:

    // Test data
    $input = <<<_DATA_
        <li id=xyz>
          John Johnson
        <sup>1<sup>
        ","
        </li>
    _DATA_;
    
    //Create a DOM object
    $html = new simple_html_dom();
    // Load HTML from a string
    $html->load($input);
    
    // >> Long answer
    echo "Long answer:<br/>";
    
    // Search all text nodes inside the target node
    $search = $html->find('li#xyz text');
    
    // Loop through each node and print it
    foreach( $search as $i => $txt ) {
        // No need to specify "->plaintext" since the content is already in plain text here
        echo "$i => " . $txt->plaintext . "<br/>";
    }
    
    // >> Short answer
    echo "<hr>";
    echo "Short answer:<br/>";
    
    // Specifying the index (0th here) returns the Nth element from the array containing all search results
    echo $html->find('li#xyz text', 0)->plaintext;
    
    // Clear DOM object
    $html->clear();
    unset($html);
    

    有关更多详细信息,请查看

    非常有用!谢谢
    Var john=document.getElementById("grabIt").innerText;
    
    // Test data
    $input = <<<_DATA_
        <li id=xyz>
          John Johnson
        <sup>1<sup>
        ","
        </li>
    _DATA_;
    
    //Create a DOM object
    $html = new simple_html_dom();
    // Load HTML from a string
    $html->load($input);
    
    // >> Long answer
    echo "Long answer:<br/>";
    
    // Search all text nodes inside the target node
    $search = $html->find('li#xyz text');
    
    // Loop through each node and print it
    foreach( $search as $i => $txt ) {
        // No need to specify "->plaintext" since the content is already in plain text here
        echo "$i => " . $txt->plaintext . "<br/>";
    }
    
    // >> Short answer
    echo "<hr>";
    echo "Short answer:<br/>";
    
    // Specifying the index (0th here) returns the Nth element from the array containing all search results
    echo $html->find('li#xyz text', 0)->plaintext;
    
    // Clear DOM object
    $html->clear();
    unset($html);
    
    Long answer:
    0 => John Johnson 
    1 => 1
    2 => "," 
    3 => 
    -------------------
    Short answer:
    John Johnson