Regex PHP: Find Everything in Div

regex php: find everything in div

What about something like this :

$str = <<<HTML
<div class="gallery">text to extract here</div>
<div class="gallery">text to extract from here as well</div>
HTML;

$matches = array();
preg_match_all('#<div[^>]*>(.*?)</div>#s', $str, $matches);

var_dump($matches[1]);

Note the '?' in the regex, so it is "not greedy".

Which will get you :

array
0 => string 'text to extract here' (length=20)
1 => string 'text to extract from here as well' (length=33)

This should work fine... If you don't have imbricated divs ; if you do... Well... actually : are you really sure you want to use rational expressions to parse HTML, which is quite not that rational itself ?

Regex to select all the content within the div

You can't. You should use DomDocument for this situation. That makes your job a lot easier.

You can load the HTML in the Dom, loop through all Div with getElementsByTagName method and filter what you need.

How to get content from a div using regex

If there are no other HTML tags inside the div, then this regex should work:

$v = '<div class="fck_detail">Some content here</div>';
$regex = '#<div class="fck_detail">([^<]*)</div>#';
preg_match($regex, $v, $matches);
echo $matches[1];

The actual regex here is <div class="fck_detail">([^<]*)</div>. Regexes used in PHP also need to be surrounded by some other character that doesn't occur in the regex (I used #).

However, if what you're parsing is arbitrary HTML provided by the user, then preg_match simply can't do this. Full-fledged HTML parsing is beyond the ability of any regex, and that's what you'll need if you're parsing the output of a full-fledged HTML editor.

Extract Content of a Div with PHP and Regex

Here is a way to get it using DOM parser:

<?php
$html = '<div class="my-class additional-class"><div class="my-class2">
<div class="my-class"></div>
</div>
</div>';

$doc = new DOMDocument();
$doc->loadHTML($html); // loads your html
$elems = $doc->getElementsByTagName('div'); // find all div elements
$outerdiv = $elems->item(0); // outermost div
echo $outerdiv->childNodes[0]->C14N() . "\n"; // print inner HTML

/*
<div class="my-class2">
<div class="my-class"></div>
</div>
*/
?>

If you really want a regex solution then use:

~<div[^>]*>(.*)</div>~is

and grab capture group #1.

Regex for getting everything inside a div including divs

You should not parse html with regex.It is bound to fail somewhere.For your problem you can use Recursive feature of php.

<div\b(?:(?R)|(?:(?!<\/?div).))*<\/div>

See demo.

https://regex101.com/r/vD5iH9/15

Regex to find html div class content and data-attr? (preg_match_all)

Why not use a DOM parser instead?

You could use an XPath expression like //div[@class or @data-id] to locate the elements then extract their attribute values

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

$xpath = new DOMXpath($doc);
$divs = $xpath->query('//div[@class or @data-id]');
foreach ($divs as $div) {
$matches = [$div->getAttribute('class'), $div->getAttribute('data-id')];
print_r($matches);
}

Demo ~ https://eval.in/1046227

regex php - find things in div with specific ID

Your code works for me if you change the enclosing doublequotes around $intro to single quotes:

$intro = '<div id="user-sub-summary">Summary</div>
<div id="user-sub-commhome"><em>Commercial</em></div>
<div id="whatever">whatever</div>';

$regex = '#\<div id="user-sub-commhome"\>(.+?)\<\/div\>#s';
preg_match($regex, $intro, $matches);
$match = $matches[0];
echo $match;

You might want to read some famous advice on regular expressions and HTML.

php - regex to match div tags

You can use the Ungreedy modifer (U), and also - do not use .*, but [^>]* (which means anything that is not > as > is the end of the tag and you are searching withing the tag). You don't need to escape / when this is not your delimiter (you are using # as delimiter)

preg_match('#(<div[^>]*id=[\'|"]'.$key.'[\'|"][^>]*>)(.*)</div>#isU', $contents, $response);


Related Topics



Leave a reply



Submit