【问题标题】:PHP Getting text between HTML nodesPHP 在 HTML 节点之间获取文本
【发布时间】:2014-12-18 20:40:27
【问题描述】:

标题说明一切。如何使用 PHP 获取 HTML 节点之间的文本?有任何想法吗? 下面是我的 HTML 结构。

<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="outer">
        <div id="first">
            <p class="this">Hello</p>
            <p class="this">Community</p>
        </div>
        <div id="second">
            <p class="that">Stack</p>
            <p class="that">Overflow</p>
        </div>
    </div>
</body>

预期输出:

HelloStackOverflowCommunity

【问题讨论】:

标签: javascript php html arrays regex


【解决方案1】:

这很简单,在这里获取 PHP Simple HTML DOM Parser:http://sourceforge.net/projects/simplehtmldom/files/

然后使用以下代码:

/* include simpledom*/
include('simple_html_dom.php');

/* load html string */
$html_string = <<<HTML
<html>
<head>
    <title>Test Page</title>
</head>
<body>
    <div id="outer">
        <div id="first">
            <p class="this">Hello</p>
            <p class="this">Community</p>
        </div>
        <div id="second">
            <p class="that">Stack</p>
            <p class="that">Overflow</p>
        </div>
    </div>
</body>
</html>
HTML;

/* create simple dom object from html */
$html = str_get_html($html_string);

/* find all paragraph elements */
$paragraph = $html->find('div[id=outer] div p');

/* loop through all elements and get inner text */
foreach($paragraph as $p){
    echo $p->innertext;
}

干杯,

罗伊

【讨论】:

    【解决方案2】:

    我建议你使用 PHP 内置的 DOMDocument 而不是像 simplehtmldom 这样的第三方类。

    在大型 HTML 文件上,它们真的很慢(我曾使用过它们)。

    <?php
    $html ='
    <html>
    <head>
        <title>Test Page</title>
    </head>
    <body>
        <div id="outer">
            <div id="first">
                <p class="this">Hello</p>
                <p class="this">Community</p>
            </div>
            <div id="second">
                <p class="that">Stack</p>
                <p class="that">Overflow</p>
            </div>
        </div>
    </body>
    ';
    
    // a new dom object
    $dom = new domDocument; 
    $dom->preserveWhiteSpace = false;
    
    // load the html into the object
    $dom->loadHTML($html); 
    // get the body tag
    $body = $dom->getElementsByTagName('body')->item(0);
     // loop through all tags
    foreach($body->getElementsByTagName('*') as $element ){
        // print the textValue
        print $element->firstChild->textContent;
    }
    

    输出将是HelloCommunity StackOverflow

    【讨论】:

      【解决方案3】:

      强烈建议不要使用正则表达式来解析 HTML。
      使用简单的 HTML 库:http://sourceforge.net/projects/simplehtmldom/files/simplehtmldom/
      包括它:include 'simple_html_dom.php';
      获取您需要的标签:$tags = $html-&gt;find('p');
      创建数组:$a = array(); foreach ($tags as $tag) $a[] = $tag-&gt;innertext;;
      创建你的字符串:$string = $a[0] . $a[2] . $a[3] . $a[1];

      【讨论】:

        【解决方案4】:

        你可以试试:

        $text = strip_tags($html);
        

        http://www.php.net/manual/en/function.strip-tags.php

        这会让你走得很远。它会留下空格并返回,但很容易删除。

        $clean = str_replace(array(' ',"\n","\r"),'',$text);
        

        http://www.php.net/manual/en/function.str-replace.php

        在你的例子中使用它给出:

        TestPageHelloCommunityStackOverflow
        

        如果您想保留一些空格,您可以尝试:

        $clean = trim(implode('',explode("\n",$text)));
        

        导致:

        Test Page Hello Community Stack Overflow
        

        可能有许多变化。

        【讨论】:

          【解决方案5】:

          试试这个

          function getTextBetweenTags($string, $tagname)
           {
              $pattern = "/<$tagname>(.*?)<\/$tagname>/";
              preg_match($pattern, $string, $matches);
              return $matches[1];
           }
          

          你必须遍历 $matches 数组...

          【讨论】:

          • @jurgemaister 该死的,打败我!
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-07-22
          • 2011-09-18
          • 1970-01-01
          • 1970-01-01
          • 2011-12-19
          • 1970-01-01
          相关资源
          最近更新 更多