删除=和?的最佳方法从PHP中的get方法? [英] Best way to remove = and ? from get method in PHP?

查看:119
本文介绍了删除=和?的最佳方法从PHP中的get方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

删除=和的最佳方法是什么?从PHP的get方法获取URL?我正在研究网站的分页结构,目前这是URL的样子:

What's the best way to remove the = and ? in a URL from the get method in PHP? I'm working on the pagination structure of my website and currently this is what the URLs look like:

www.example/test/?page=3

我希望它看起来像这样:

I want it to look like this:

www.example/test/3

是否可以使用一些额外的代码直接在PHP get方法中解决此问题,还是必须通过htaccess文件完成?

Can this be addressed directly in the PHP get method with some extra code or does it have to be done through an htaccess file?

这是我的htaccess文件现在的样子:

This is what my htaccess file looks like right now:

RewriteBase /  
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]


推荐答案

这是我的尝试,我从涉及URL提取的不同StackOverflow主题中整理了一些已经存在的答案,然后提出了以下解决方案:

Here is my try, I compacted some already existing answer from different StackOverflow topics dealing with URL extraction and I came up with this solution :

<?php
    function http_protocol() {
        /**
         * @see https://stackoverflow.com/a/6768831/3753055
         */
        return (isset($_SERVER['HTTPS']) ? 'https' : 'http') . '://';
    }

    function http_host() {
        return $_SERVER['HTTP_HOST'];
    }

    function http_uri() {
        return parse_url($_SERVER["REQUEST_URI"], PHP_URL_PATH);
    }

    function http_refactored_query_strings() {
        $queries = explode('&', $_SERVER['QUERY_STRING']);
        $refactoredQueries = [];

        foreach( $queries as $query ) {
            $refactoredQueries[] = filter_var(explode('=', $query)[1], FILTER_SANITIZE_STRING);
        }

        $queries = implode('/', $refactoredQueries);

        return $queries ?: '';
    }

    function http_refactored_url() {
        return http_protocol() . http_host() . http_uri() . http_refactored_query_strings();
    }

    echo http_refactored_url();
?>

尝试使用一些示例:

  • GET http://example.com/test.php : http://example.com/test.php
  • GET http://example.com/test.php?page=3 : http://example.com/test.php/3
  • GET https://example.com/test.php?view=page?page=3 : https://example.com/page/3
  • GET http://example.com/?page=3 : http://example.com/3

对于查询字符串重构部分,我使用了 $ _ SERVER ['QUERY_STRING] 并将值分解为& 字符。因为第一个不包含在 $ _ SERVER ['QUERY_STRING'] 中。因此,您想出了很多数组,其中包含 page = 3 view = page 等字符串,以及foreach,请使用 = 分隔符将其拆分,然后获取第二个元素(索引: 1 )以将其附加到解决方案。

For the query string refactoring part, I used $_SERVER['QUERY_STRING] and exploded the value to & character. because the first ? is not contained in $_SERVER['QUERY_STRING']. So you come up with lot of arrays, containing strings like page=3, view=page, ... And foreach one, you split it using = delimiter and get the second element (index : 1) to append it to the solution.

希望有帮助

这篇关于删除=和?的最佳方法从PHP中的get方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆