Skip to content

Instantly share code, notes, and snippets.

@mgng
Created October 7, 2014 02:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgng/470cb69c77214c2169dc to your computer and use it in GitHub Desktop.
Save mgng/470cb69c77214c2169dc to your computer and use it in GitHub Desktop.
twitter user_id <=> screen_name convert
<?php
$user_id = "5168731";
$screen_name = "mugng";
var_dump(
u2s( "5168731" ), // string(5) "mugng"
s2u( "mugng" ) // string(7) "5168731"
);
/**
* user_id 2 screen_name
* @param string $user_id
* @return string
*/
function u2s( $user_id ) {
preg_match( "/class=\"nickname\">@(.+?)</uis", file_get_contents("https://twitter.com/intent/user?user_id={$user_id}"), $m );
return isset( $m[1] ) ? $m[1] : null;
}
/**
* screen_name 2 user_id
* @param string $screen_name
* @return string
*/
function s2u( $screen_name ) {
preg_match( "/content=\"twitter\:\/\/user\?id=(.+?)\"/uis", file_get_contents("https://twitter.com/intent/user?screen_name={$screen_name}"), $m );
return isset( $m[1] ) ? $m[1] : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment