简体   繁体   English

PHP的正则表达式检测到一切,即使换行

[英]php regex detect everything even newline

this is my input : 这是我的输入:

<div class="entry-content">
    <p> Hey ! </p>
    <h2> How Are You ?! </h2>
</div><!-- .entry-content -->

and this is my RegEx ! 这是我的RegEx!

"<div class=\"entry-content\">(.*?)</div><!-- .entry-content -->"

this work when there is no line between <div> tag like this 像这样在<div>标记之间没有行时,这项工作

<div class="entry-content"> Hey ! </div><!-- .entry-content -->

But i need actually everything even new line other html tags and etc. 但是我实际上需要所有东西,甚至换行其他html标记等。

You should use XML Parsing framework like DOM to parse XML documents (including HTML), but if you really need to use regex (assuming PCRE) there's an s PCRE modifier : 您应该使用XML解析框架喜欢DOM解析XML文档(包括HTML),但如果你真的需要使用正则表达式(假设PCRE)有一个s PCRE修改

s (PCRE_DOTALL)

If this modifier is set, a dot metacharacter in the pattern matches all characters, including newlines. 如果设置了此修饰符,则模式中的点元字符将匹配所有字符,包括换行符。 Without it, newlines are excluded. 没有它,换行符将被排除。 This modifier is equivalent to Perl's /s modifier. 此修饰符等效于Perl的/ s修饰符。 A negative class such as [^a] always matches a newline character, independent of the setting of this modifier. 否定类(例如[^ a])始终与换行符匹配,而与该修饰符的设置无关。

So you may write: 所以你可以这样写:

$matches = array();
preg_match_all("~<div class=\"entry-content\">(.*?)</div><!-- \\.entry-content -->~s",
    $text, $matches);

BTW: Here's an example for you how to use DOM to fetch elements based on their class name. 顺便说一句: 这是为您提供一个示例该示例说明如何使用DOM根据元素的类名获取元素。

Use the right tool for the job instead of trying to parse this using a regular expression. 为作业使用正确的工具 ,而不要尝试使用正则表达式对其进行解析。

$html = <<<DATA
<div class="entry-content">
    Hey !
    How Are You ?!
</div><!-- .entry-content -->
DATA;

$dom = new DOMDocument;
$dom->loadHTML($html);

$xpath = new DOMXPath($dom);
$node  = $xpath->query('//div[@class="entry-content"]');

echo $node->item(0)->nodeValue;

Output 输出量

    Hey !
    How Are You ?!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM