Get parameters from <a> tag

How can i get all parameters from an <a> tag?

need to have the following result
$content = ‘class=“idz” href=“http://www.domain1.com”’;

$content = 'asdasd <a class="idz" href="http://www.domain1.com">View this</a> hhh ';

$pattern = '/#<a(.*)>View this</a>#ims/'; 
$content = preg_replace($pattern, '$1', $content);

var_dump(content);

I did:

$content = '<a href="http://localhost" class="links" id="home">Home</a>'; 

$pattern = '/<[a]{1}(.*)?>(.*)<\\/[a]{1}>/';   // sloppy regex
$content = trim(preg_replace($pattern, '$1', $content)); 

$attributes = explode(" ", $content);
$attrArray = array();

foreach ($attributes as $attribute)
{
	list($attr, $value) = explode("=", $attribute, 2);
	$attrArray[$attr] = str_replace("\\"", "", $value);
}

and the resulting array looked like:


Array
(
    [href] => http://localhost
    [class] => links
    [id] => home
)

Hi Zurev,

Many thanks, seems it is almost working…

How can only check for Home link
$pattern = ‘/<[a]{1}(.*)?>(Home)<\/[a]{1}>/’; // sloppy regex
seems an error in this regex

$content = '<a href="http://localhost" class="links" id="home">Home</a>... <a href="http://localhost/links" class="links" id="links">Links</a>'; 

$pattern = '/<[a]{1}(.*)?>(Home)<\\/[a]{1}>/';   // sloppy regex
$content = trim(preg_replace($pattern, '$1', $content)); 

$attributes = explode(" ", $content);
$attrArray = array();

foreach ($attributes as $attribute)
{
    list($attr, $value) = explode("=", $attribute, 2);
    $attrArray[$attr] = str_replace("\\"", "", $value);
} 


var_dump($attrArray);

DOMDocument is probably a safer bet:

$html = '<a href="http://localhost" class="links" id="home">Home</a>... <a href="http://localhost/links" class="links" id="links">Links</a>'; 
$searchWord = 'Home';
$attributes = array();
$parameterString = '';

$doc = new DOMDocument(); 
$doc->loadHTML($html);

foreach($doc->getElementsByTagName('a') as $link) {
    if ($link->nodeValue == $searchWord) {
        foreach($link->attributes as $attr) {
            $attributes[$attr->name] = $attr->value;
            $parameterString .= ' '.$attr->name.'="'.$attr->value.'"';
        }
    }
}

// show results
echo '<pre>';
print_r($attributes);
print_r($parameterString);
echo '</pre>';