从PHP中的url获取数字
我有以下网址:从PHP中的url获取数字,php,regex,Php,Regex,我有以下网址:http:example.com/country/France/45。 使用模式http:example.com/country/name/**NUMBER**(?$\u尽可能获取) 如何使用正则表达式(或其他正则表达式)提取数字?使用explode(): php的get命令是$\u get以便显示要执行的操作 <html> <body> <?php echo $_GET["eg"]; ?> </body> </html>
http:example.com/country/France/45
。
使用模式http:example.com/country/name/**NUMBER**(?$\u尽可能获取)
如何使用正则表达式(或其他正则表达式)提取数字?使用explode()
:
php
的get命令是$\u get
以便显示要执行的操作
<html>
<body>
<?php
echo $_GET["eg"];
?>
</body>
</html>
URL为http:example.com/country/name/?eg=**NUMBER**
且带有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);
$str='http:example.com/country/France/45';
preg_match('/http:example\.com\/country\/(?P\w+)\/(?P\d+)/,$str,$matches);
打印($matches);//返回数组(“name”=>“France”,“id”=>45);
使用类似的方法
$url = "http://example.com/country/France/45";
$parts = explode('/', $url);
$number = $parts[count($parts) - 1];
如果你在最后得到了变量,你可以像这样进一步爆炸
$number = explode('?', $number);
$number = $number[0];
希望这有帮助:)
简单不是吗
trim()
的用法是删除尾部的\
echo $last = substr(strrchr($url, "/"), 1 );
strrchr()
将给出/
字符的最后一次出现,然后substr()
在它之后给出字符串。\/country\/.\/.\/([0-9]+)使用preg\u match尝试此操作\u all如果没有get参数且url没有以/
结尾,则此操作将失败!我想你应该说$number=$parts[count($parts)-1]
$url = 'http:example.com/country/France/45';
$id = end(explode('/',trim($url,'/')));
echo $last = substr(strrchr($url, "/"), 1 );