Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
从PHP中的url获取数字_Php_Regex - Fatal编程技术网

从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 );