Get anchor tag value in PHP

In this article, you going to see how to get anchor tag value in PHP with simple way and step by step.

If you are looking for this topic then you are at a right place.

With the help of 3 methods we going to understand this topic in a simple way.

Let’s start with the topic, to get anchor tag value in PHP.

In this first method declare form with input fields in which one input is hidden with a value of anchor tag.

Whatever the value you require you can add into the input type hidden field.

As i have require Href value, So i have added Href value in hidden input field.

So as per your requirement you can add value in hidden put field and on button click of a button click you will get anchor tag value on a form post.

From below script you come to understand regarding, how one can get anchor tag value.

<?php 
	if($_POST['Submit'] == 'Submit'){
		echo "Achor tag value is as follow as :<br/>";
		echo $_POST['anchorValue'];
	}
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Get anchor tag value in PHP</title>
	<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
</head>
<body>
<div style="background-color:#e5bcbc;" align="center">
	<h1 align="center">Get anchor tag value in PHP On Button Click</h1>
	<form action="" method="post">
	<input name="anchorValue" id="anchorValue" type="hidden" value="htt://abc.com" >
	<input name="textValue"  id="textValue" type="text" value="Enter text to change" >
	<a href="htt://abc.com">ABC</a>
	<input name="Submit" id="Submit" type="Submit" value="Submit" >
	</form>
</div>
</body>
</html>

OUTPUT:

Anchor tag Value
Anchor tag Value

Second Method to get anchor value

In second method, you going to see how using preg_match_all() function one can get anchor tag value.

preg_match_all(pattern, input, matches, flags, offset)

Syntax for preg_match_all() function are as follow as:

TitleDescription
patternIt is Required. It consists of a regular expression that indicates what to search.
inputIt is Required. It consists of a string for which the search will be performed.
matchesIt is Optional. This variable used to populated with an array containing all of the matches that were found.
preg function

Below is the code snippet from which you can understand regarding use of preg_match_all() function.

<?php

echo "Get anchor tag value in PHP - Using preg_match_all function<br/><br/>";  
$str = "<a href='http://www.xyz.com' title='this is techone blog' alt='techone'>Techone</a>";
preg_match_all('/<a .*?>(.*?)<\/a>/',$str,$matches);
echo '<pre>';
print_r($matches);    /* For All matches*/
//For Text Name
echo $matches[1][0]; //output : Techone
?>

In the third method you will see how one can get anchor tag value using file function called file_get_contents() function.

The file_get_contents() function reads a file into a string.

Below is the syntax for file_get_contents() function.

file_get_contents(path, include_path, context, start, max_length)
TitleDescription
pathIt is Required. It Specifies the path to the file which has to read
include_pathIt is Optional. Set this parameter ‘1’ to 1 if you want to search for a file.
contextIt is Optional. Represent the context.
startIt is Optional. Represent from where in the file to start reading.
max_lengthIt is Optional. Represent maximum length of data read and the default is EOF
file get function

Below is PHP file which is called in file_get_contents() function.

// event.php file which is called in file get function.
<html><head></head><body>
<a href="something" name="something">Anchor value</a>
<a href="something2" name="something2">Anchor value2</a>
<a href="something3" name="something3">Anchor value3</a>
</body></html>

After calling it read the file and get the content of the file.

In that anchor tag is declare.

Using for each loop you will get all anchor tag value

Textbox textchanged event in PHP

Below is code snippet from which you will come to understand successfully.

<?php
echo "Get anchor tag value in PHP - Using file function<br/><br/>"; 
$sfile = file_get_contents('event.php'); // loads file to string
$html = new DOMDocument; // is object class DOMDocument
$html->loadHTML($sfile); // loads html
$nodelist = $html->getElementsByTagName('a'); // nodes
foreach ($nodelist as $node) {
  echo $node->nodeValue, "<br />\n"; }
?>
<?php 
	if($_POST['Submit'] == 'Submit'){
		echo "Achor tag value is as follow as :<br/>";
		echo $_POST['anchorValue'];
	}
?>

OUTPUT:
Anchor value
Anchor value2
Anchor value3

Conclusion :

Finally, we done with the point of how one can get anchor tag value in PHP using different method.

I hope you like this article and if you feel like we missed out on anything, please comment below.

Leave a Comment