ItSolutionStuff.com

How to get HTML Tag Attribute Value in PHP?

By Hardik Savani • May 14, 2024
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: PHP
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

How to Upgrade PHP Version from 7.4 to 8 in Ubuntu?

Read Now →

PHP Array Get Previous Element from Current Key

Read Now →

PHP Explode Every Character into Array Example

Read Now →

PHP Remove Element from Array by Value Example

Read Now →

PHP Curl Request with Username and Password Example

Read Now →

PHP Curl Delete Request Example Code

Read Now →

PHP Curl Get Request with Parameters Example

Read Now →

How to Upgrade PHP Version from 7.3 to 7.4 in Ubuntu?

Read Now →

Laravel Bootstrap Tag System Example Tutorial

Read Now →

HTML Tags are not Allowed in Textbox Validation using JQuery

Read Now →

How to Get User IP Address in PHP?

Read Now →