PHP – Get the Text Between a Given Tag

The given PHP example helps you to get the text between given tag.

Code Example:
function getTextBetweenTags($string, $tagname) {
    $pattern = "/<$tagname ?.*>(.*)<\/$tagname>/i";
    preg_match($pattern, $string, $matches);
    return $matches[1];

$string = '<textformat leading="2"><p align="left"><font size="10">get me out form here</font></p></textformat>';
$txt = getTextBetweenTags($string, "font");
echo $txt;

Post to Twitter Post to Digg Post to Facebook Post to Google Buzz Send Gmail

Leave a Comment

Your email address will not be published. Required fields are marked *