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.

2

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

Comments

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 ?!

1 Comment

that's Great thanks , i will try it later, but now i need to get html tags too , nodeValue is Just Text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.