如何使用正则表达式从具有其他 HTML 标记的 Div 获取内容


How to get content from Div which have other HTML tags using Regexp

我有包含其他html tags的div以及text

我只想从所有html tags中的这个div或中提取文本

<div class="rpr-help m-chm">
                <div class="header">
                    <h2 class="h6">Repair Help</h2>
                </div><!-- /end .header -->
                <div class="inner m-bsc">
                    <ul>

                        <li><a href="#videol">Repair Video</a></li>
                        <li><a href="#qa1">Repair Q&amp;A</a></li>
                    </ul>
                </div>
                    <div>
                    <br>
                    <span class="h4">Cross Reference Information</span><br>
                    <p>Part Number 285753A (AP3963893) replaces  1195967, 280152, 285140, 285743, 285753, 3352470, 3363664, 3364002, 3364003, 62672, 62693, 661560, 80008, 8559748, AH1485646, EA1485646, PS1485646.
                    <br>
                    </p>
                    </div>
            </div>

这是我的Regexp

preg_match_all("/<div class='"rpr-help m-chm'">(.*)<'/.*>/s", $urlcontent, $description);

每当我将这个完整的div分配给$urlcontent变量时,它都可以正常工作。

但是当我从真实 url 获取数据时,例如$urlcontent = "www.test.com/test.html"; 它返回完整的网页脚本。

如何进入<div class="rpr-help m-chm">内容?

我的正则表达式中是否有任何更正要求?

任何帮助将不胜感激。谢谢

通过正则表达式解析 HTML/XHTML 是不可能的。源

你不能用正则表达式解析 [X]HTML。因为 HTML 无法解析 正则表达式。正则表达式不是可用于正确解析 HTML 的工具

根据您使用的语言,请考虑使用第三方库进行 HTML 解析。

use this function
    function GetclassContent($tagStart,$tagEnd,$content)
    {
        $first_step = explode( $tagStart,$content );
        $second_step = explode($tagEnd,$first_step[1] );
        return $second_step[0];
    }
Steps to Use Above function 
$website="www.test.com/test.html";
$content=file_get_contents($website);
$tagStart ='<div  class="rpr-help m-chm">';
$tagEnd   = "</div >";
$RequiredContent = GetclassContent($tagStart,$tagEnd,$content);