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

用php隔离url的一部分

用php隔离url的一部分,php,Php,如果我有以下url: http://URL/products/38/293/bannana_cake/ <pre> <?php print_r($_GET);?> </pre> 或 如何从上面的示例中分离出bannana_蛋糕和水果蛋糕?这将是一个$\u GET变量,因为您看到的只是一个查询字符串的mod\u重写版本 尝试以下操作以查看变量名称: 它将是一个$\u GET变量,因为您看到的只是一个查询字符串的mod\u重写版本 尝试以下操作以查看变量名称:

如果我有以下url:

http://URL/products/38/293/bannana_cake/
<pre>
<?php print_r($_GET);?>
</pre>


如何从上面的示例中分离出bannana_蛋糕和水果蛋糕?

这将是一个$\u GET变量,因为您看到的只是一个查询字符串的mod\u重写版本

尝试以下操作以查看变量名称:


它将是一个$\u GET变量,因为您看到的只是一个查询字符串的mod\u重写版本

尝试以下操作以查看变量名称:



您可以通过查看$\u SERVER[]数组(通过谷歌搜索以找到确切的条目)来查看请求的URL。您可以将字符串拆分为“/”上的数组,然后[3]索引将成为您感兴趣的URL的一部分。

您可以通过查看$\u SERVER[]数组(通过谷歌搜索以找到确切的条目)来查看请求的URL。如果您可以将字符串拆分为“/”上的数组,[3]索引将是您感兴趣的URL的一部分。

将URL拆分为斜杠并检索最后一部分:

$parts = explode('/', $url);
echo ($parts[sizeof($parts) - 1])
    ? $parts[sizeof($parts) - 1]
    : $parts[sizeof($parts) - 2];
唯一的问题是,url中需要有尾随的斜杠。您可以这样检查:

<?php
echo basename("http://url/products/38/293/banana_cake/"); // Produces "banana_cake"
?>

在斜杠上拆分url并检索最后一部分:

$parts = explode('/', $url);
echo ($parts[sizeof($parts) - 1])
    ? $parts[sizeof($parts) - 1]
    : $parts[sizeof($parts) - 2];
唯一的问题是,url中需要有尾随的斜杠。您可以这样检查:

<?php
echo basename("http://url/products/38/293/banana_cake/"); // Produces "banana_cake"
?>

可能最简单的方法(仅适用于您的特殊情况,不适用于生产)是使用:


这只是因为“香蕉蛋糕”是url的最后一部分,最后一个斜杠后面没有任何内容


这绝对不是一个理想的解决方案,Luca Matteis的答案将得到我的投票,因为查询字符串顺序的最轻微更改将破坏一切。

可能最简单的方法,只适用于您的特殊情况,不适用于生产,是使用:


这只是因为“香蕉蛋糕”是url的最后一部分,最后一个斜杠后面没有任何内容


这绝对不是一个理想的解决方案,Luca Matteis的答案将得到我的投票,因为查询字符串顺序的最细微变化将破坏一切。

我的答案将稍微长一点。看起来您想做一些类似于使用的事情,所以这里有一个类(称为xs_细分)中的两个函数的片段,我有这个类来做这些事情。它可以很容易地扩展到包括通配符和条件行为(在将来的一段时间里,我的任务太少了)。第一,设置和使用的示例

// Static variable to hold our tokens
$_tokens = null ;

// Input path (set in constructor)
$_path = null ;

// Results here
$values = array() ;

function parse ( $schema = '' ) {

            // Sanitize input data : Regular Expression
            $regexp = '/[^a-z0-9 +\-\/!$*_=|.:]/i' ;

            // Break our path into little bits
            $break = explode ( '/', $this->_path ) ;

            // Find the tokens used from our schema template
            $this->_tokens = $this->getSubStrs ( "{","}", $schema ) ;

            // Loop through the path elements
            foreach ( $break as $key => $value ) {

                // Sanitize the value of the element
                $value = urldecode ( trim ( preg_replace ( $regexp, '', $value ) ) ) ;

                // Element not blank? (Meaning, real text)
                if ( $value != '' )

                    // Index it!
                    @$this->values[$this->_tokens[$key]] = $value ;

            }

        }

        function getSubStrs ( $from, $to, $str, &$result = array () ) {

            if ( strpos ( $str, $from ) !== false ) {

                $start = strpos ( $str, $from ) + 1 ;
                $end = strpos ( $str, $to ) - 1 ;

                $item = substr ( $str, $start, $end - $start + 1 ) ;
                $rest = substr ( $str, $end + 2 ) ;

                $result[] = $item ;

                $this->getSubStrs ( $from, $to, $rest, $result ) ;

            }

            return $result ;
        }
代码(仅仅是一些基本的东西,应该足以激起你自己的兴趣;所有的代码都太长了,不能在这里发布。如果你想让整个shebang包含三个嵌套的属性/JavaBean类,请给我一条消息)


我的回答会稍微长一点。看起来您想做一些类似于使用的事情,所以这里有一个类(称为xs_细分)中的两个函数的片段,我有这个类来做这些事情。它可以很容易地扩展到包括通配符和条件行为(在将来的一段时间里,我的任务太少了)。第一,设置和使用的示例

// Static variable to hold our tokens
$_tokens = null ;

// Input path (set in constructor)
$_path = null ;

// Results here
$values = array() ;

function parse ( $schema = '' ) {

            // Sanitize input data : Regular Expression
            $regexp = '/[^a-z0-9 +\-\/!$*_=|.:]/i' ;

            // Break our path into little bits
            $break = explode ( '/', $this->_path ) ;

            // Find the tokens used from our schema template
            $this->_tokens = $this->getSubStrs ( "{","}", $schema ) ;

            // Loop through the path elements
            foreach ( $break as $key => $value ) {

                // Sanitize the value of the element
                $value = urldecode ( trim ( preg_replace ( $regexp, '', $value ) ) ) ;

                // Element not blank? (Meaning, real text)
                if ( $value != '' )

                    // Index it!
                    @$this->values[$this->_tokens[$key]] = $value ;

            }

        }

        function getSubStrs ( $from, $to, $str, &$result = array () ) {

            if ( strpos ( $str, $from ) !== false ) {

                $start = strpos ( $str, $from ) + 1 ;
                $end = strpos ( $str, $to ) - 1 ;

                $item = substr ( $str, $start, $end - $start + 1 ) ;
                $rest = substr ( $str, $end + 2 ) ;

                $result[] = $item ;

                $this->getSubStrs ( $from, $to, $rest, $result ) ;

            }

            return $result ;
        }
代码(仅仅是一些基本的东西,应该足以激起你自己的兴趣;所有的代码都太长了,不能在这里发布。如果你想让整个shebang包含三个嵌套的属性/JavaBean类,请给我一条消息)


这不是将所有内容都隔离到URL右侧吗?我添加了分解功能。这不是将所有内容隔离到URL右侧吗?我添加了分解功能。不一定是$\u-GET,可能在$\u服务器['PATH\u-INFO']中,取决于重写配置不一定是$\u-GET,可能在$\u服务器['PATH\u-INFO']根据rewrite Config的不同,一个示例将很好地显示如何分割字符串等等。一个示例将很好地显示如何分割字符串等等。
   $br = new xs_Breakdown ( '{first}/{second}/{third}/{fourth}/{fifth}/{andsoon}' ) ;

   // Pick out the template variable called 'third'
   $third = $br->third ;
// Static variable to hold our tokens
$_tokens = null ;

// Input path (set in constructor)
$_path = null ;

// Results here
$values = array() ;

function parse ( $schema = '' ) {

            // Sanitize input data : Regular Expression
            $regexp = '/[^a-z0-9 +\-\/!$*_=|.:]/i' ;

            // Break our path into little bits
            $break = explode ( '/', $this->_path ) ;

            // Find the tokens used from our schema template
            $this->_tokens = $this->getSubStrs ( "{","}", $schema ) ;

            // Loop through the path elements
            foreach ( $break as $key => $value ) {

                // Sanitize the value of the element
                $value = urldecode ( trim ( preg_replace ( $regexp, '', $value ) ) ) ;

                // Element not blank? (Meaning, real text)
                if ( $value != '' )

                    // Index it!
                    @$this->values[$this->_tokens[$key]] = $value ;

            }

        }

        function getSubStrs ( $from, $to, $str, &$result = array () ) {

            if ( strpos ( $str, $from ) !== false ) {

                $start = strpos ( $str, $from ) + 1 ;
                $end = strpos ( $str, $to ) - 1 ;

                $item = substr ( $str, $start, $end - $start + 1 ) ;
                $rest = substr ( $str, $end + 2 ) ;

                $result[] = $item ;

                $this->getSubStrs ( $from, $to, $rest, $result ) ;

            }

            return $result ;
        }