Getting data using Regular Expressions - PHP - 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

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!

you can also look at the project here. PHP DomParser
This might help!

Related

Fast way to get specific data from HTML string using PHP

I avoided a lot to come here share my problem. I have googled a lot and find some solutions but not confirmed.
First I explain My Problem.
I have a CKEditor in my site to let the users post comments. Suppose a user clicks two posts to Multi quote them, the data will be like this in CKEditor
<div class="quote" user_name="david_sa" post_id="223423">
This is Quoted Text
</div>
<div class="quote" user_name="richard12" post_id="254555">
This is Quoted Text
</div>
<div class="original">
This is the Comment Text
</div>
I want to get all the elements separately in php as below
user_name = david_sa
post_id = 223423;
quote_text = This is Quoted Text
user_name = david_sa
post_id = richard12;
quote_text = This is Quoted Text
original_comment = This is the Comment Text
I want to get the data in above format in PHP. I have googled and found the preg_match_all() PHP function near to my problem, that uses the REGEX to match the string patterns. But I am not confirmed that is it a legitimate and efficient solution or there is some better solution. If You have any better solution Please Suggest Me.
You can use DOMDocument and DOMXPath for this. It takes very few lines of code to parse HTML and extract just about anything from it.
$doc = new DOMDocument();
$doc->loadHTML(
'<html><body>' . '
<div class="quote" user_name="david_sa" post_id="223423">
This is Quoted Text
</div>
<div class="quote" user_name="richard12" post_id="254555">
This is Quoted Text
</div>
<div class="original">
This is the Comment Text
</div>
' . '</body></html>');
$xpath = new DOMXPath($doc);
$quote = $xpath->query("//div[#class='quote']");
echo $quote->length; // 2
echo $quote->item(0)->getAttribute('user_name'); // david_sa
echo $quote->item(1)->getAttribute('post_id'); // 254555
// foreach($quote as $div) works as expected
$original = $xpath->query("//div[#class='original']");
echo $original->length; // 1
echo $original->item(0)->nodeValue; // This is the Comment Text
If you are not familiar with XPath syntax then here are a few examples to get you started.
You should not be using regex's to process HTML/XML. This is what DOMDocument and SimpleXML are built for.
You problem seems relatively simple, so you should be able to get away with using SimpleXML (aptly named, huh?)
Do not even try regex to parse html. I would recommend simple html dom. Get it here: php html parser

How to get and print attributes in meta tags using simple_html_dom

Ok, I am at a loss here. I am new to programming, have Googled for this relentlessly, and have even asked about this here but to no avail, so I am trying again now that I am a little better researched.
I need to retrieve the text from what is inside the "content" attribute from this line of code:
<meta property='og:title' content="title goes here" />
This is the code that I have so far that is not working:
//saves the URL as HTML
$movie_html = file_get_html($movie_url);
//finds the title within the HTML
$movie_title_meta = $movie_html->find("meta[name=og:title]");
$movie_title = $movie_title_meta->content;
//prints the score
var_dump($movie_title_meta);
What I want that code to give me is:
title goes here
But instead I get a NULL error. I have a feeling a var_dump() is not actually what I want to be doing, but a simple echo() is returning errors also. I just have no idea what to do at this point, so any help would be GREATLY appreciated.
Thanks in advance!
Try using the PHP Simple HTML DOM Parser by David Walsh...
http://davidwalsh.name/php-notifications

Inserting content within HEREDOC using Functions

Been trying to go bout how to accomplish dividing content on view output.
I'm loading records from Mysql, inserting values into HEREDOC to then output to view.
I want to display only a certain amount of characters in a particular row within the HEREDOC, hide the rest from view on DOM.
I tried using a function like this within the HEREDOC to insert a "More" link after certain amount of characters.
I've tried a couple methods:
1
<<<EOT
<div id="$id_op">
{substr_replace($contents, "More", 400)}
</div>
EOT;
2
<<<EOT
<div id="$id_op">
{${substr_replace($contents, "More", 400)}}
</div>
EOT;
There might be other better methods to accomplish this. For now I want to load the record completely into dom but hide part of it until user have clicked a jQuery selector.
Any help / direction will be truly appreciated.
Functions does not execute in HEREDOC. This will work
$div = substr_replace($contents, "More", 400);
$data = <<<EOT
<div id="$id_op">
$div
</div>
EOT;
Compute any variable content before starting the HEREDOC, then insert the variables into the string as you go.

Select first DOM Element of type text using phpQuery

Let's say i have this block of code,
<div id="id1">
This is some text
<div class="class1"><p>lala</p> Some markup</div>
</div>
What I would want is only the text "This is some text" without the child element's .class1 contents. I can do it in jquery using $('#id1').contents().eq(0).text(), how can i do this in phpQuery?
Thanks.
my bad, i was doing
pq('#id1.contents().eq(0).text()')
instead of
pq('#id1')->contents()->eq(0)->text()
If compatibility is what you are after, and you want to traverse/manipulate elements as DOM objects, then perhaps the PHP DOM XML library is what you are after: http://www.php.net/manual/en/book.domxml.php
Your code would look something like this:
$xml = xmldoc('<div id="id1">This is some text<div class="class1"><p>lala</p> Some markup</div></div>');
$node = $xml->get_element_by_id("id1");
$content = $node->get_content();
I'm sorry, I don't have time to run a test of this right now, but hopefully it sets you in the right direction, and forms the basis for a decent revision... There is a good list of DOM traversal functions in the PHP documentation though :)
References: http://www.php.net/manual/en/book.domxml.php, http://www.php.net/manual/en/function.domdocument-get-element-by-id.php, http://www.php.net/manual/en/function.domnode-get-content.php

How can i get some part of html using XPath?

I need to get some part of html pages using php XPath query (sorry im new to XPath).
example html page
<html><head>blah lines</head><body>
<div id="mytable">
<table><tr><td>table cell value</td></tr></table>
</div>
<body><html>
is there any way to get div contents of above example along with html tags using XPath
The following xpath query will return the "mytable" div:
//div[#id='mytable']
//div will select all divs in the document
[#id='mytable'] will return only matches with a "mytable" attribute
/html/body/div[#id='mytable']
thank you all, but i was need whole part including tags, so i solved as follows
$detail= $xpath->query("//div [#id='mytable']");
$querycontent = $detail->item(0);
$html->saveXML($querycontent);

Categories