0

this is my input :

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

and this is my RegEx !

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

this work when there is no line between <div> tag like this

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

But i need actually everything even new line other html tags and etc.

Mojtaba Yeganeh
  • 2,788
  • 1
  • 30
  • 49
  • 1
    possible duplicate of [RegEx match open tags except XHTML self-contained tags](http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags) – Wooble Jun 30 '14 at 13:33
  • Take a look about the s modifier here: http://www.regular-expressions.info/modifiers.html and try to use DOMDocument instead. – Casimir et Hippolyte Jun 30 '14 at 13:34

2 Answers2

2

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:

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. A negative class such as [^a] always matches a newline character, independent of the setting of this modifier.

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.

Community
  • 1
  • 1
Vyktor
  • 20,559
  • 6
  • 64
  • 96
1

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 ?!
hwnd
  • 69,796
  • 4
  • 95
  • 132