Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/237.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_Zend Framework - Fatal编程技术网

用php检查URL的最后一部分

用php检查URL的最后一部分,php,zend-framework,Php,Zend Framework,我正在使用Zend Framework。我想检查链接的最后一部分。我的链接是http://localhost/sports/soccer/page_id/776543233242 我的URL的最后一部分必须有12或11位数字,我希望该部分的第一部分以8开头,如果是11位,则以7开头,如果是12位 public function detailAction () { $uri = Zend_Controller_Front::getInstance()->getRequest()-&

我正在使用Zend Framework。我想检查链接的最后一部分。我的链接是
http://localhost/sports/soccer/page_id/776543233242

我的URL的最后一部分必须有12或11位数字,我希望该部分的第一部分以8开头,如果是11位,则以7开头,如果是12位

public function detailAction ()
{

    $uri = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
     if (substr(substr($uri, -12),1)=='7'){
        echo "successfull";
     }
     else if (substr(substr ($uri , -11),8)=='0'){
        echo "succ";
     }
     else {
        echo "failed";
     }
}

这应该适合您:

<?php

       $url = "http://localhost/sports/soccer/page_id/776543233242";
       $part = basename($url);

       if(strlen($part) == 11 && $part[0] == 8 || strlen($part) == 12 && $part[0] == 7)
            echo "yes";
        else
            echo "no";

?>
看。然后,您可以获取路径并在
/
上拆分它

function isValidUrl($url)
{
    $elements = parse_url($url);

    if ($elements === false) {
        return false;
    }

    $lastItem = end(explode("/", $elements['path']);
    return ((strlen($lastItem) == 12 && $lastItem[0] == '7') || (strlen($lastItem) == 11 && $lastItem[0] == '8'));
}
在php中使用
basename()
函数获取url的最后一部分,然后计算字符串。使用
strlen
检查字数。使用下面的代码

    public function detailAction ()
{

    $url = Zend_Controller_Front::getInstance()->getRequest()->getRequestUri();
           $url = "http://localhost/sports/soccer/page_id/776543233242";
           $basename = basename($url);

           if(strlen($basename) == 11 && $basename[0] == 8 || strlen($basename) == 12 && $basename[0] == 7){
                echo "succ";
    }
            else{
                echo "failed";
    }
    }

希望这能帮助你

如果你只想从你的url中获取数字,我建议你使用regex。在这种情况下:

$url="http://localhost/sports/soccer/page_id/776543233242";
$regex = "/[0-9]+/";
preg_match_all($regex, $url, $out); //$out will be an array storing the mathcing strings, preg_match_all creates this variable to us
var_dump($out);
此代码输出
$out
。它将如下所示:

array(1) {
[0]=>
    array(1) {
    [0]=>
        string(12) "776543233242"
    }
}

我希望它能帮助您。

显然,所需的url部分对应于
页面id
参数

使用Zend Framework,您可以执行以下操作以获取此参数的值:

$page_id = $this->getRequest()->getParam('page_id');

if(strlen($page_id) == 11 && $page_id[0] == 8 
|| strlen($page_id) == 12 && $page_id[0] == 7)
    echo "yes";
else
    echo "no";

所以你只想抓取:
776543233242
并验证它?实际上我想与其他字符串进行比较,但在这一步中,是的,只验证这个子字符串。@Rizier123我希望我的答案对你有帮助,这就是你想要的为什么这看起来与我的答案如此相似?!哦这几乎和你的答案一样,我没有注意到,再见,你的答案是+1
$page_id = $this->getRequest()->getParam('page_id');

if(strlen($page_id) == 11 && $page_id[0] == 8 
|| strlen($page_id) == 12 && $page_id[0] == 7)
    echo "yes";
else
    echo "no";