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 “”. 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 <>';

echo getContentBetween($email, '<', '>');  //returns

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive