Skip to content

Instantly share code, notes, and snippets.

@hatsumatsu
Last active June 27, 2016 09:44
Show Gist options
  • Save hatsumatsu/805db5bd6e4f5a4b744dc2e677330674 to your computer and use it in GitHub Desktop.
Save hatsumatsu/805db5bd6e4f5a4b744dc2e677330674 to your computer and use it in GitHub Desktop.
/**
* Extract the twitter user name from a twitter URL
* @param [type] $user_name [description]
* @return [type] [description]
*/
public static function extract_username_from_url( $user_name ) {
// remove https://twitter.com/
$user_name = str_replace( 'https://twitter.com/', '', $user_name );
$user_name = str_replace( 'https://www.twitter.com/', '', $user_name );
// remove http://twitter.com/
$user_name = str_replace( 'http://twitter.com/', '', $user_name );
$user_name = str_replace( 'http://www.twitter.com/', '', $user_name );
// without protocol
$user_name = str_replace( 'twitter.com/', '', $user_name );
$user_name = str_replace( 'www.twitter.com/', '', $user_name );
// remove '@'
$user_name = str_replace( '@', '', $user_name );
// remove '/''
$user_name = str_replace( '/', '', $user_name );
return $user_name;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment