Validate facebook profile URL

Asked

Viewed 703 times

0

4 answers

5


Something more or less like this:

function fbUser( $url ) {
    preg_match('~^https?://(www.)?facebook.com/(profile\.php\?id=)?(.*)~',$url,$matches);
    return $matches[3];
}

See working on IDEONE.


Understanding the 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

It follows an even more robust version, which separates the parameters from the query string and ignores ? and / in Urls with name:

function fbUser( $url ) {
   $rgx='~^https?://(www\.)?facebook.com/(?|profile\.php\?(?:.*&)*id=([^&]*)|([^/\?]*))~';
   if( preg_match( $rgx, $url, $matches ) ) return $matches[2];
}

See working on IDEONE.


This solution used our master’s suggestion in Regex, @Guillhermelautert, to use branch reset (alternative groups), with the syntax (?| )

  • 1

    Remember that anything you can use (?|) to force a group to be able to get more than one :D result

  • 1

    @NGTHM4R3 now I think it looks pretty cool: http://ideone.com/8C6Fc3

  • Thank you, I’ll test.

1

Basically using parse_url() and parse_str() of to make with ease.

The ID or null if it is not the facebook url or cannot 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 it solves.

0

Returning the user id has a certain difficulty, if it is the public name the above way will solve you if you have the url input. If you need the real user ID, I suggest using the gift and manipulating the profile image class: "profilePicThumb" and picking up the string between "fbid=" and "&set". This is the id used by Facebook, for Ads campaign purposes does not result in anything, since the face deleted the option to raise Id-based bases. I hope I’ve helped.

  • It would not be to use any API, just to get the profile id same.

0

There goes everything in a single REGEX, 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 domain name, if not nouver this everything else is already disregarded in the script

$resultadodominioface[2] -> The name of the guy in the URL is not required because the script doesn’t matter because it analyzes his existence or not.

$resultadodominioface[3] -> The id of the guy

In this case I endorsed the variable ($resultadodominioface[1]), because if there is not a valid domain all the rest will be invalid, because everything depends on it. In this case the regular expression allows:

http://facebook.com/fulano OR https://facebook.com/fulano

http://facebook.com/profile.php?id=123789123456777 OR https://facebook.com/profile.php?id=123789123456777

http://www.facebook.com/fulano OR https://www.facebook.com/fulano

http://www.facebook.com/profile.php?id=123789123456777 OR https://www.facebook.com/profile.php?id=123789123456777

I did it now and I tried a lot! Thanks! I don’t need the script but if I need it, thanks to you, it’s ready! Thanks!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.