+44 (0)785 715 1348
office@bogdans.co.uk

WordPress: get the text between a href anchor tags 0

Posted on 11, September 2013

in Category Web development


This is what you have to do in order to get the content between the a href / anchor tags:

$out_vector = getTextBetweenTags(‘a’, $out);

function getTextBetweenTags($tag, $html, $strict=0){
/*** a new dom object ***/
$dom = new domDocument;

/*** load the html into the object ***/
if($strict==1)
{
$dom->loadXML($html);
}
else
{
$dom->loadHTML($html);
}

/*** discard white space ***/
$dom->preserveWhiteSpace = false;

/*** the tag by its tag name ***/
$content = $dom->getElementsByTagname($tag);

/*** the array to return ***/
$out = array();
foreach ($content as $item)
{
/*** add node value to the out array ***/
$out[] = $item->nodeValue;
}
/*** return the results ***/
return $out;
}

0 Comments