How to get value of a tag in php

this is simple html code that i want to get sorry, promise and back-link values from that

<div id="core">
  <h2 id="sorry">Sorry, something went wrong.</h2>
  <p id="promise">
    We're working on it and we'll get it fixed as soon as we can.
  </p>
  <p id="back-link">
    <a id="back" href="https://www.facebook.com/">Go Back</a>
  </p>
  <div id="footer">
    Facebook
    <span id="copyright">
      © 2018
    </span>
    <span id="help-link">
      ·
      <a id="help" href="https://www.facebook.com/help/">Help Center</a>
    </span>
  </div>
</div>

with below code i get this result for one of them, for example:

DOMElement Object
(
    [tagName] => p
    [schemaTypeInfo] => 
    [nodeName] => p
    [nodeValue] => 
        Go Back

    [nodeType] => 1
    [parentNode] => (object value omitted)
    [childNodes] => (object value omitted)
    [firstChild] => (object value omitted)
    [lastChild] => (object value omitted)
    [previousSibling] => (object value omitted)
    [nextSibling] => (object value omitted)
    [attributes] => (object value omitted)
    [ownerDocument] => (object value omitted)
    [namespaceURI] => 
    [prefix] => 
    [localName] => p
    [baseURI] => 
    [textContent] => 
        Go Back

)

but its only object of all attributes and it's not what i want to have that and i want to get sorry, promise and back-link values from this html

my code:

<?php
$url = "http://localhost/error.html";
$html = file_get_contents($url);
$doc = new DOMDocument(); // create DOMDocument
libxml_use_internal_errors(true);

$doc->loadHTML($html); // load HTML you can add $html
//libxml_clear_errors();

if ($html) {
    $divs = $doc->getElementById('core');

    $xpath = new DOMXPath($doc);

    $backLink = $xpath->query('//p[@id="back-link"]');

    //$sorryId = $xpath->query('//h2[@id="sorry"]');
    //$promiseId = $xpath->query('//p[@id="promise"]');


    $node = $backLink->item(0);
    $href = $node->getAttribute('href');
    echo '<pre>';print_r($node);echo '</pre>';
}
?>

please help me to fix this code to get values from html, Thanks in advance

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 value of a tag 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

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: 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=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, 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:

  • 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.

How can get HTML tag value in PHP?

How to Get HTML Tag Value in PHP?.
Hello, In this example, i will show you php get html tag value example. ... .
index.php. $htmlEle = "<html><body><p id='paragraphID'>This is ItSolutionStuff.com Example</p></body></html>"; ... .
Output: ... .
index.php. ... .
Read Also: How to Upgrade PHP Version from 7.4 to 8 in Ubuntu?.

How to get input value from user in PHP?

To get input from users, you also have to prompt them to enter something. You can use PHP's `readline() function to get this input from the console.

How to get the value from a form field with PHP?

You can get the value $value as : $value = $_POST['subject']; or: $value = $_GET['subject']; ,depending upon the form method used.

How can I call HTML ID in PHP?

PHP cannot "call an HTML control". If you want to dynamically modify things in the browser, you'll have to do it client-side using Javascript. It is client-side, so you have to use Javascript, not PHP.