How to Get HTML Tag Value in PHP?

By Hardik Savani May 14, 2024 Category : PHP

Hello,

In this example, i will show you php get html tag value example. We will look at example of php get html element value by id. In this article, we will implement a php get html element value. This post will give you simple example of how to get html tag value in php.

Here, i will give you simple two examples how to get html tag value in php and another how to get value by id in php. so let's see both code and output as bellow:

PHP Get HTML Element Value

index.php

<?php

$htmlEle = "<html><body><p id='paragraphID'>This is ItSolutionStuff.com Example</p></body></html>";

$domdoc = new DOMDocument();

$domdoc->loadHTML($htmlEle);

$pTagValue = $domdoc->getElementById('paragraphID')->nodeValue;

echo $pTagValue;

Output:

This is ItSolutionStuff.com Example

PHP Get HTML Element Value By ID

index.php

<?php

$htmlEle = "<html><body><p>This is ItSolutionStuff.com Example 1</p><p>This is ItSolutionStuff.com Example 2</p></body></html>";

$domdoc = new DOMDocument();

$domdoc->loadHTML($htmlEle);

$pTags = $domdoc->getElementsByTagName('p');

foreach ($pTags as $p) {

echo $p->nodeValue, PHP_EOL;

}

Output:

This is ItSolutionStuff.com Example 1

This is ItSolutionStuff.com Example 2

i hope it can help you...

Tags :
Shares