【问题标题】:PHP get the <h[1-6]></h[1-6]> values from an html textPHP 从 html 文本中获取 <h[1-6]></h[1-6]> 值
【发布时间】:2016-09-10 05:14:20
【问题描述】:

在我的代码中,我有以下正则表达式:

 preg_match_all('/<title>([^>]*)<\/title>/si', $contents, $match );

从网页中检索 &lt;h&gt;..&lt;/h&gt; 标记。但有时它可能有 html 标签,例如 &lt;strong&gt;,&lt;b&gt; 等,因此它需要一些修改,因此我尝试了这个

preg_match_all('/<h[1-6]>(.*)<\/h[1-6]>/si', $contents, $match );

但是出了点问题,没有检索到 html &lt;h&gt; 标签中的内容。

你能帮我正确修改正则表达式吗?

【问题讨论】:

  • 如果hs 有任何属性,这将失败。 .* 也很贪心,如果页面上有多个,它会吃掉所有东西。解析器是您最好的方法。看看stackoverflow.com/questions/3577641/…
  • 正如在另一篇文章中所说,不要使用正则表达式来解析 HTML,除非您的 html 非常简单并且您不需要搜索嵌套标签。即使那样,也是个坏主意。有 DOM 解析器 (DOMDocument) 用于解析 HTML,并且非常易于使用。它们有几种与 JS 相同的方法,例如 getElementsByTagName,可用于查找每个 &lt;h&gt; 标签。

标签: php html web-scraping html-parsing


【解决方案1】:
preg_match_all('<h\d>', $contents, $matches);

foreach($matches as $match){
$num[] = substr ( $match  , 1 , 1 );
}

【讨论】:

    【解决方案2】:

    现在,这里没有正则表达式专家,但他应该站在你的立场上吗?他会这样做:

        <?php
    
            // SIMULATED SAMPLE HTML CONENT - WITH ATTRIBUTES:
            $contents = '<section id="id-1">And even when darkness covers your path and no one is there to lend a hand;
                <h3 class="class-1">Always remember that <em>There is always light at the end of the Tunnel <span class="class-2">if you can but hang on to your Faith!</span></em></h3>
                <div>Now; let no one deceive you: <h2 class="class-2">You will be tried in ever ways - sometimes beyond your limits...</h2></div>
                <article>But hang on because You are the Voice... You are the Light and you shall rule your Destiny because it is all about<h6 class="class4">YOU - THE REAL YOU!!!</h6></article>
                </section>';
    
            // SPLIT THE CONTENT AT THE END OF EACH <h[1-6]> TAGS   
            $parts      = preg_split("%<\/h[1-6]>%si", $contents);
            $matches    = array();
    
            // LOOP THROUGH $parts AND BUNDLE APPROPRIATE ELEMENTS TO THE $matches ARRAY.       
            foreach($parts as $part){
                if(preg_match("%(.*|.?)(<h)([1-6])%si", $part)){
                    $matches[] = preg_replace("%(.*|.?)(<)(h[1-6])(.*)%si", "$2$3$4$2/$3>", $part);
                }
            }
            var_dump($matches);
    
    
            //DUMPS::::
            array (size=3)
              0 => string '<h3 class="class-1">Always remember that <em>There is always light at the end of the Tunnel <span class="class-2">if you can but hang on to your Faith!</span></em></h3>' (length=168)
              1 => string '<h2 class="class-2">You will be tried in ever ways - sometimes beyond your limits...</h2>' (length=89)
              2 => string '<h6 class="class4">YOU - THE REAL YOU!!!</h6>' (length=45)
    

    作为一个函数,它归结为:

     <?php
    
            function pseudoMatchHTags($htmlContentWithHTags){
                $parts      = preg_split("%<\/h[1-6]>%si", $htmlContentWithHTags);
                $matches    = array();
                foreach($parts as $part){
                    if(preg_match("%(.*|.?)(<h)([1-6])%si", $part)){
                        $matches[] = preg_replace("%(.*|.?)(<)(h[1-6])(.*)%si", "$2$3$4$2/$3>", $part);
                    }
                }
                return $matches;
            }
    
            var_dump(pseudoMatchHTags($contents));
    

    你可以在这里测试它:https://eval.in/571312 ...也许它有点帮助...我希望... ;-)

    【讨论】:

      【解决方案3】:

      当使用(.*) 时,您可以获取所有内容,仅针对单词、数字和空格,也许您可​​以使用范围与它们一起获取一个或多个:

      preg_match_all('/<h[1-6]>([\w\d\s]+)<\/h[1-6]>/si', $contents, $match);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多