2014-11-16 132 views
0

我需要从不同的URL获取一个人的名字。如何从URL,PHP,Wordpress获取名称

例子:

URL可以从http://www.example.com/John-Smith/

http://www.example.com/some-other-info/hellen-leroy/

到www.example.com/some/other/info/nick-waller/

不同,但它并没有限制到这些变化(只有人的姓名保持格式firstname-姓氏)

我需要把名称在这样一个变量中的人:

$ personName =“Firstname Lastname”;

我该怎么做? P.S. :可以使用纯PHP或一些Wordpress功能完成。

+0

以使用**爆炸的URL)的最后部分(**,然后用** str_replace()函数**替换 - 与空间'。您可能还想检查** ucfirst()**函数。 –

回答

1
$temp = explode('/', $_SERVER['REQUEST_URI']); 
$temp = explode('-', $temp[count($temp)-1]; 
$firstName = $temp[0]; 
$lastName = $temp[1]; 

希望帮助:)