How to Get HTML Tag Value in PHP

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.

How to Get HTML Tag Value in PHP
How to Get HTML Tag Value in PHP

Here we discuss 4 tasks where we use the PHP DOMDocument class to get the value of HTML tags,

  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

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 = new DOMDocument();
$domdoc->loadHTML($htmlEle);

echo $domdoc->getElementById('paraID')->nodeValue;

Output: Paragraph Sports

Compiler: https://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 = new DOMDocument();
$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 = new DOMDocument();
$domdoc->loadHTML($htmlEle);

$spanValue = $domdoc->getElementById('SpanID')->nodeValue;

//Store span Value in PHP variable
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, https://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:

Happy Coding..!

Was this article helpful?
YesNo
My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH, my native place in Odisha. I am a Web Programmer by profession and working on more than 50 projects to date. My passion is working on the web-based project using PHP and relate to all CMS and frameworks which is based on PHP.
Posts created 163

7 thoughts on “How to Get HTML Tag Value in PHP

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
mariobet - supertotobet - escort -

takip7.com

We are now accepting Guest Posting and creation of Backlinks
This is default text for notification bar