Untitled UI logotext
Solutions
WebsitesEcommerceMobile AppsWeb AppsProduction Support & Maintenance
Our work
Company
About usBlogPodcastContact us
Book a free consultation

PHP Get Content Between Characters

Olivia Rhye

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

‍

Ready to start a project?

Book a free consultation
Untitled UI logotext
Our work
About us
Blog
Careers
Submit a ticket
Agency Partnerships
Contact
© 2024 fjorge. All rights reserved.
Privacy