Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/294.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 必须创建捕获第一个preg_replace bar url的规则_Php - Fatal编程技术网

Php 必须创建捕获第一个preg_replace bar url的规则

Php 必须创建捕获第一个preg_replace bar url的规则,php,Php,必须创建捕获第一个preg_replace bar url的规则: http://localhost/item/other 无论后面是什么,都必须只接受规则“item”看起来您没有特定的问题。我假设你要写一些脚本 $request_uri = explode('/', $_SERVER['REQUEST_URI']); $delimiter = array_shift($request_uri); $controller = array_shift($request_ur

必须创建捕获第一个preg_replace bar url的规则:

http://localhost/item/other


无论后面是什么,都必须只接受规则“item”

看起来您没有特定的问题。我假设你要写一些脚本

    $request_uri = explode('/', $_SERVER['REQUEST_URI']);
    $delimiter = array_shift($request_uri);
    $controller = array_shift($request_uri);
    $action = array_shift($request_uri);
    if(preg_match('/item/i', $controller))
    {
        // do something right
    }
    else 
    {
        // do something wrong
    }
您应该使用php函数

例如:

$parts = parse_url("http://localhost/item/other");
$path_parts= explode('/', $parts[path]);
$item = $path_parts[1];
echo $item;

正确,是routes脚本,但我无法在此阶段设置操作,只是需要在出现另一个提示之前或之后恢复/item/而不进行任何操作?谢谢你喜欢让事情变得困难,不是吗?3 X阵列_移位???尝试使用诸如parse_url()之类的内置函数来帮助用户,这就是它们存在的原因。您知道其中的不同吗?使用
array\u shift
可以避免编写验证过程,因为
array\u shift
返回移位值,如果array为空或不是数组,则返回NULL。当然,如果您假定每个输入/url都不需要验证,则不需要应用此方法,这将导致“无害”的应用程序。另外,
parse_url()
不适用于相对url,并且专门用于解析url而不是URI(或者在本例中,
$\u SERVER['REQUEST_URI']
参数),因此此处不匹配使用。