从 Google Plus 个人资料网址获取用户 ID 或名称


Getting User ID or Name from Google Plus Profile Url

GooglePlus个人资料网址可以写成:

https://plus.google.com/117302453758282711096/
https://plus.google.com/u/0/105604861727423445668
https://plus.google.com/u/1/105604861727423445668
https://plus.google.com/+tuando

在 php 甚至一般的正则表达式中帮助他人,我如何解析此 url 以返回用户名/用户 ID?

在这种情况下

,"用户名/用户ID"是URL的最后一部分。
使用parse_url函数从 url 路径中获取最后一部分:

$url1 = "https://plus.google.com/u/1/105604861727423445668/";
$url2 = "https://plus.google.com/+tuando";
$path = parse_url(trim($url1,"/"), PHP_URL_PATH);
echo "<pre>";
$last_section = end(explode("/", $path));
var_dump($last_section);   // string(21) "105604861727423445668"

// checking $url2
$path = parse_url(trim($url2,"/"), PHP_URL_PATH);
echo "<pre>";
$last_section = end(explode("/", $path));
var_dump($last_section);   // string(7) "+tuando"

http://php.net/manual/en/function.parse-url.php

可能的正则表达式:plus.google.com/(('+'w+)|((|u'/(1|0)'/)'d{21}))

将匹配 $2 中的用户名或 $4 中的 id

假设 id 是一串 21 个数字...