The following PHP code snippet extracts the twitter username from twitter URL using regular expressions. For example, if you pass the following url, it will output ‘Stacktips‘.

twitter.com/Stacktips

Snippet:

if ( !function_exists( 'get_twitter_id_from_url' ) ){
	function get_twitter_id_from_url($url)
	{	
  	  if (preg_match("/^https?:\/\/(www\.)?twitter\.com\/(#!\/)?(?<name>[^\/]+)(\/\w+)*$/", $url, $regs)) {
  	    return $regs['name'];
  	  }
  	  return false;	  
  }
}
Tagged in : #PHP

Nilanchala

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Join The Discussion

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.