Possible Duplicate:
How to get text between div tags with id being 1st attribute. Only with regular expressions.
Here is the scenario
I’m using this regular expression for getting the text inside div tag with id test1,or tes2 or test3
<div id = "test1">text</div>
<div id = "test2">text</div>
<div id = "test3">text</div>
$id_value = "test1" or "test2" or "test3";
$regex = "#\<div id=\"".$id_value."\"\>(.+?)\<\/div\>#s";
My only requirement is to get the text from div tags in below scenario
<div id="test" class="testing" style="color:red" etc etc .... more attributes >text</div>
i.e id being the 1st attribute of div tag and can be followed by n number of attributes. How to extract text from such a tag by regex only.
I even tried
$regex = '#<div\s+.*?id="".$id_value."".*?>(.*?)</\s*div>#ims';
it is returning text of div tag when $id_value = “test1″ but if $id_value=”test2″ it is returning text values of both nodes test1 and test2. If $id_value=”test3” it is returning text value of all 3 nodes. I need text value related to specific id only. Using Regex only.
Please help Thanks You.
Don’t use RegExp to parse HTML. Instead, use PHP’s DOM extension which can correctly parse any kind of HTML.
Example:
If and only if you absolutely must use RegExp, this is what I came up with:
Beware, if you use this code, the unh̶oly͘ ͘c̀h̶i͏l҉d wį
ll weep the blood of virgins. The
<center> cannot hold it is too late.