我有以下url: http:example.com/country/France/45
。模式为http:example.com/country/name/**NUMBER**(?$_GET possibly)
.
如何使用regex(或其他regex)提取数字?
With regexp:
$str = 'http:example.com/country/France/45';
preg_match('/http:example.com/country/(?P<name>w+)/(?P<id>d+)/', $str, $matches);
print_r($matches); // return array("name"=>"France", "id" => 45);
$url = 'http:example.com/country/France/45';
$id = end(explode('/',trim($url,'/')));
简单不是吗?
trim ()
的作用是去除尾随的
echo $last = substr(strrchr($url, "/"), 1 );
strrchr()
将给出/
字符最后出现的位置,然后substr()
在其后面给出字符串。
使用如下命令
$url = "http://example.com/country/France/45";
$parts = explode('/', $url);
$number = $parts[count($parts) - 1];
如果末尾有GET变量,可以像这样展开
$number = explode('?', $number);
$number = $number[0];
希望有帮助
php
的get命令是$_GET
,所以显示数字do
<html>
<body>
<?php
echo $_GET["eg"];
?>
</body>
</html>
URL为http:example.com/country/name/?eg=**NUMBER**
的使用explode()
:
$parts = explode('/', $url);
$number = $parts[count($parts)-1];