How can I validate a facebook profile URL and get the user id? Examples:
And the other variations:
htttps://facebook
www.facebook
And get the value of the id, from the examples, would be:
walterwhite and xxxxx
Something like this:
function fbUser( $url ) {
preg_match('~^https?://(www.)?facebook.com/(profile\.php\?id=)?(.*)~',$url,$matches);
return $matches[3];
}
See working at IDEONE .
Understanding RegEx:
~^https?://(www.)?facebook.com/(profile\.php\?id=)?(.*)~ Essa é a RegEx completa.
~ ~ Delimitadores de começo e fim
^ Indica o começo da linha
s? (www.)? (profile\.php\?id=)? Campos opcionais
http :// facebook.com/ Caracteres fixos (obrigatórios)
(.*) retornamos este valor
Here is an even more robust version, which separates parameters from query string and ignores ?
and /
in named URLs:
function fbUser( $url ) {
$rgx='~^https?://(www\.)?facebook.com/(?|profile\.php\?(?:.*&)*id=([^&]*)|([^/\?]*))~';
if( preg_match( $rgx, $url, $matches ) ) return $matches[2];
}
See working at IDEONE .
This solution used our RegEx master's suggestion, @GuilhermeLautert , to use branch reset (alternative groups), with the syntax (? |)
Basically using parse_url () and parse_str () to do it with ease.
Return ID
or null
if it is not the facebook url or can not detect the id.
function getFbId($url){
$fbId = null;
$info = parse_url($url);
if( isset($info['host']) && preg_match('/facebook\.com/', $info['host']) ){
if(isset($info['query'])){
$qs = parse_str($info['query'], $params);
if( isset($params['id']) ){
//pega o id aqui $params['id']
$fbId = $params['id'];
}
}else if( isset($info['path']) ){
$path = str_replace('/', '', $info['path']);
//precisa ajustar essa expressao, não lembro exatamente se isso tudo é permitido ou falta algo
if(preg_match('/\.php/', $path)==0){
if( preg_match('/([a-zA-Z0-9]|\.|_)+/', $path)>0 ){
$fbId = $path;
}
}
}
}
return $fbId;
}
var_dump(getFbId('http://www.facebook.com/walterwhite'));
var_dump(getFbId('http://www.facebook.com/profile.php?id=xxxxx'));
I confess that it is not the most beautiful function in the world but solves it.
Returning the user id has a certain difficulty, if it is the public name, the form above will solve you if you have the url input. If you need the actual user ID I suggest using the gift and manipulating the profile image class: "profilePicThumb" and picking the string between "fbid=" and "& set". This is the id used by facebook, for Ads campaign purposes will not turn out anything, since the face has excluded the option to upload bases based on Id. I hope I have helped.
There goes everything in a single REGEX, I finished now:
//Retirar possíveis espaços na digitação
$odominioface = preg_replace('/([\s]+)/i', '', $_POST['link_do_facebook_profile']);
preg_match('/^(http(?:s)?[\:][\/][\/](?:www[\.])?facebook[\.]com[\/])(?:([a-z0-9\-\_]+)|(?:profile[\.]php[\?]id[\=]([0-9]{15})))$/i', $odominioface, $resultadodominioface);
if((isset($resultadodominioface[1]) == true) and ($resultadodominioface[1] != null)){
if((isset($resultadodominioface[2]) == true) and ($resultadodominioface[2] != null)){
echo "Puts grilla bicho eu achei o nome do perfil na URL! O nome é: ".$resultadodominioface[2]."<br />";
} elseif((isset($resultadodominioface[3]) == true) and ($resultadodominioface[3] != null)){
echo "Não encontrei o nome do perfil, mas pelo menos achei o id! O id é: ".$resultadodominioface[3]."<br />";
} else{
echo "Que mer* bicho! Não encontrei nada!<br />";
}
}
THE VARIABLES:
$resultadodominioface[1] ->
Returns the name of the domain, if not nouver that everything else is already disregarded in the script
$resultadodominioface[2] ->
The name of the guy in the URL, is not mandatory because the script does not matter because it analyzes the existence of it or not.
$resultadodominioface[3] ->
The id of the face
In case I evaluated the variable ($ resultadodominioface [1]), because if there is no valid domain, the rest will be invalid, because everything depends on it. In this case the regular expression allows:
I've done it now and tested a lot! Thank you! I do not need the script but if I need to, thanks to you, it's ready! Thanks!