<?php
function getSocialUser( $uri ) {
$parsed = parse_url( $uri );
$path = $parsed[ 'path' ];// Usuário, mas com alguns caracteres que devem ser filtrados
$host = str_replace( 'www.', '', $parsed[ 'host' ] );// Retiramos o 'www.'
$host_name = explode( '.', $host )[ 0 ];// Separamos pelo '.' e pegamos a primeira parte
$allowed_hosts = [ 'twitter', 'facebook', 'instagram' ];// Hosts permitidos
if ( !in_array( $host_name, $allowed_hosts ) )
return false;// Se não estiver entre os permitidos, retorne falso (ou exception)
return strtok( ltrim( $path, '/' ), '/' );
}
$url1 = 'https://www.facebook.com/gilberto.gil';
$url2 = 'http://instagram.com/gilberto.gil/posts';
$url3 = 'https://twitter.com/gilberto.gil?parametro_qualquer=123';
$url4 = 'https://google.com/gilberto.gil?parametro_qualquer=123';
echo ( getSocialUser( $url1 ) ?: 'Inválido' ) . "\n";
echo ( getSocialUser( $url2 ) ?: 'Inválido' ) . "\n";
echo ( getSocialUser( $url3 ) ?: 'Inválido' ) . "\n";
echo ( getSocialUser( $url4 ) ?: 'Inválido' ) . "\n";
preferences:
27.92 ms | 409 KiB | 5 Q