Get text between HTML tags – PHP

In this post, I will show you how to get text between HTML tags like span, label etc.

function getTextBetweenHtmlTags($string, $tagname) {    
$pattern = "/<$tagname>(.*?)</$tagname>/";   
preg_match($pattern, $string, $matches);   
return $matches[1];
}


function truncate_str($str, $maxlen) {
    if (strlen($str) <= $maxlen) {
       return $str;
    }  
    $newstr = substr($str, 0, $maxlen);
    if ( substr($newstr,-1,1) != ' ' ) 
        $newstr = substr($newstr, 0, strrpos($newstr, " "));
    return $newstr;

}

Posted in: PHP