PHP Get Content Between Characters

January 21, 2011

Recently, I needed to get the content of an email address formatted as “Tim Barsness ” as just the email address “email@address.com”. I wrote a function to do so. It ended up being pretty straight forward.

function getContentBetween($content, $lead, $trail)
{
	//Gets content after last occurrence of $lead as $between_split[count($from_split)]
	$between_split = split($lead, $content);
	//Gets content before $trail as $between_split[0]
	$between_split = split($trail, $between_split[count($from_split)]);
	return $between_split[0];
}

Which can be used as follows:

$email = 'Tim Barsness <email@address.com>';

echo getContentBetween($email, '<', '>');  //returns email@address.com

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive