Guns
Guns

Reputation: 2728

Getting data using Regular Expressions - PHP

I have html text stored in one of the columns of a database. the column name is mailbody and the table name is inbox_master.

Some the cells of column mailbody has divs like below

<div id="uid-g-uid" style="">2802</div>

or

<div id="uid-g-uid">
<p class="MsoNormal">6894</p>
</div>

or

<div id="uid-g-uid" style="display:none;">
6894</div>

what is common here is a div with the id "uid-g-uid". I want to be able to read the html of this div. I know this could be done using regular expressions however, not sure how to do it.

Below is the regex that i have tried but doesnt work all the time

/(?<=\<div\ id\=\"uid\-g\-uid\").*?(?=\<\/div\>)/gim

Upvotes: 0

Views: 141

Answers (2)

user2928120
user2928120

Reputation: 130

you can also look at the project here. PHP DomParser

This might help!

Upvotes: 1

Guns
Guns

Reputation: 2728

Thanks to @sikfire and @dave, i got the solution using DOM. below is my working which helped me

$doc = new DOMDocument();
@$doc->loadHTML('The HTML Content Goes here');
$d = $doc->getElementById('uid-g-uid');
echo 'Value is ' . $d['textContent'];

Didnt knew this could be this simple! Thanks Guys!

Upvotes: 4

Related Questions