How can I get the content from the <p> tag inside a p tag with ID categories using DOMDocument?
Here is the code:
<?php ini_set('max_execution_time', 300); //error_reporting(0); $errmsg_arr = array(); $errflag = false; function getState($string) { $ex = explode(" ",$string." "); return $ex[1]; } $xml = ""; $xml .= '<?xml version="1.0" encoding="UTF-8" ?>'; $xml .= ' <tv generator-info-name="www.mysite.com/xmltv">'; $baseUrl = file_get_contents('www.myscript.com/get-listing.php'); $domdoc = new DOMDocument(); $domdoc->strictErrorChecking = false; $domdoc->recover=true; $domdoc->loadHTML($baseUrl); ?>For example, my HTML might look like:
<p id='categories'>Sports</p>I'm trying to extract the following text:
SportsSo when I tried this:
$p = $domdoc->getElementById('categories')->getElementsByTagName('p')->item(0);echo $p;
It will not extract the content from the id categories.
How can I do it?
asked Jul 24, 2016 at 14:45
2
For that html only, you can do it like this:
echo $domdoc->getElementById('categories')->nodeValue;Output:
Sports
See it in action here.
Since you can't have a <p> tag inside <p> tag, DOMDocument kind goes crazy about it. Depending on the case, you could do a workaround like this:
$baseUrl = ' <p id="categories"> <p>Sports</p> </p>'; $baseUrl = str_replace('<p', '<div', $baseUrl); $baseUrl = str_replace('<p>', '<div>', $baseUrl); $baseUrl = str_replace('</p>', '</div>', $baseUrl); $domdoc = new DOMDocument(); $domdoc->loadHTML($baseUrl); echo $domdoc->getElementById('categories')->getElementsByTagName('div')->item(0)->nodeValue;The output is the same as before.
answered Jul 24, 2016 at 15:00
FirstOneFirstOne
5,6715 gold badges25 silver badges45 bronze badges
1
In this particular, How to Get HTML Tag Value in PHP, article we use the PHP DOMDocument Class which represents the HTML or XML document also serves as the root of the document tree.
Here we discuss 4 tasks where we use the PHP DOMDocument class to get the value of HTML tags,
- How to get a paragraph <p> tag value using PHP
- How to get <span> tag value using PHP
- How to store <span> value in PHP
- How to access HTML element in PHP
In This Article
- 1 How to get a paragraph <p> tag value using PHP
- 2 How to get <span> tag value using PHP
- 3 How to Store span Value in PHP
- 4 How to Access HTML Element in PHP
- 4.1 Related
How to get a paragraph <p> tag value using PHP
On the below example How to get p tag value using PHP, we create a paragraph with HTML ID attribute and set the text inside the P tag.
<?php $htmlEle="<p id='paraID'>Paragraph Sports</span>"; $domdoc=newDOMDocument(); $domdoc->loadHTML($htmlEle); echo$domdoc->getElementById('paraID')->nodeValue; |
Output: Paragraph Sports
Compiler: //phpcoder.tech/php-online-editor/
Code Explanations:
- Using the PHP DOMDocument Class, call the DOMDocument object.
- Call the predefined loadHTML() function with variable parameters.
- Using the getElementById() DOM function we get the HTML element value.
How to get <span> tag value using PHP
<?php $htmlEle="<span id='SpanID'>Span Sports</span>"; $domdoc=newDOMDocument(); $domdoc->loadHTML($htmlEle); echo$domdoc->getElementById('SpanID')->nodeValue; |
Output: Span Sports
This is also the same process and the code with changed ID.
How to Store span Value in PHP
If you want to store only span value then do the same code and create a variable on the last and assign the previous value what you get using PHP DOMDocument.
<?php $htmlEle="<span id='SpanID'>Span Sports</span>"; $domdoc=newDOMDocument(); $domdoc->loadHTML($htmlEle); $spanValue=$domdoc->getElementById('SpanID')->nodeValue; <strong>//Store span Value in PHP variable</strong> echo$spanValue; |
Output: Span Sports
How to Access HTML Element in PHP
Here is complete quick summary of how to get Html tag value in PHP or access HTML element,
- getElementsByTagName(‘a’)
- getElementById(‘SpanID’)
For more you can check on the officials site, //www.php.net/manual/en/class.domdocument.php
Here is a complete reference about access the HTML element in PHP with an example.
Also Check:
- How to Convert JSON to Array PHP
- How to Store Data In Cookies Using JavaScript
- Steps to Upload Multiple Images in PHP with Preview
- Multiple Barcode Generator in PHP
Happy Coding..!
Was this article helpful?
YesNo
Bikash
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.