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
Read the Longest String from an Array in PHP 5.3
Serial Comm with PHP on Windows
How to Detect If Your Website Visitor Came from a Google Search Result
Format 32-Character String with Hyphens to Become Uuid
Any PHP Code to Detect the Browser with Version and Operating System
Processing Multidimensional JSON Array with PHP
Mysql_Fetch_Array Add All Rows
Best Way to Handle Dirty State in an Orm Model
PHP MySQL SQL Parser (Insert and Update)
Client Side VS Server Side Basics
Multidimensional Arrays Nested to Unlimited Depth
Warning: MySQLi_Query() Expects at Least 2 Parameters, 1 Given. What
Is There a Function Similar to Settimeout() (Javascript) for PHP