How to get HTML Tag Attribute Value in PHP?

By Hardik Savani November 5, 2023 Category : PHP

In this article we will cover on how to implement php get html tag attribute value. Here you will learn how to get html tag attribute value in php. This article goes in detailed on php how to get html tag attribute value. i would like to share with you php regex get html tag attribute value. Here, Creating a basic example of php get custom attribute value from html.

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

Example:

index.php

<?php

$htmlEle = "<html><body><p data-name='call'>This is ItSolutionStuff.com Example 1</p><p data-name='call2'>This is ItSolutionStuff.com Example 1</p></body></html>";

$domdoc = new DOMDocument();

$domdoc->loadHTML($htmlEle);

$xpath = new DOMXpath($domdoc);

$query = "//p[@data-name]";

$entries = $xpath->query($query);

foreach ($entries as $p) {

echo $p->getAttribute('data-name'), PHP_EOL;

}

Output:

call

call2

i hope it can help you...

Tags :
Shares