先贴代码
PHP代码
- <?php
- /*函数url_parse用于解析url,使用时需要在前面定义
- *define('PATH_INFO',true);
- *需要解析时调用该函数,取值方法用回$_GET['xxx']即可
- */
- function url_parse()
- {
- if (!(isset($_SERVER['PATH_INFO']) && defined('PATH_INFO'))) {
- return;
- }
- $url = substr($_SERVER['PATH_INFO'], 1);
- $url = str_replace(array("'", '"', '.htm', '.html'), array('', '', '', ''), $url);
- $url = explode('/', $url);
- $param_count = count($url);
- for ($i = 0; $i < $param_count; $i += 2) {
- if (isset($url[$i + 1]) && !is_numeric($url[$i])) {
- $_GET[$url[$i]] = $url[$i + 1];
- }
- }
- unset($param_count, $url, $i);
- }
- ?>
说明:
www.yogool.cn/index.php/category/ajax 。(ajax后面可加.htm)这样,用$_GET['category']即可取出ajax.
再如 www.yogool.cn/index.php/category/ajax/page/2.htm,这样用$_GET['page']可取到2;它的页面相当于www.yogool.cn/index.php?category=ajax&page=2
淡水以前也写过,不过不是没写成函数,且要考虑到图片和css路径的问题.
原文:http://www.yogool.cn/index.php?controller=Default&action=ShowArticle&aid=10
更新一下:
PHP代码
- <?php
- /*函数url_parse用于解析url,使用时需要在前面定义
- *define('PATH_INFO',true);
- *需要解析时调用该函数,取值方法用回$_GET['xxx']即可
- */
- function url_parse() {
- if (!(isset ($_SERVER['PATH_INFO']) && defined('PATH_INFO'))) {
- return;
- }
- $url= substr($_SERVER['PATH_INFO'], 1);
- $url= str_replace(array (
- "'",
- '"',
- '.html',
- '.htm'
- ), array (
- '',
- '',
- '',
- ''
- ), $url);
- $url= explode('/', $url);
- $param_count= count($url);
- for ($i= 0; $i < $param_count; $i += 2) {
- if (isset ($url[$i +1]) && !is_numeric($url[$i])) {
- $_GET[$url[$i]]= $url[$i +1];
- }
- }
- unset ($param_count, $url, $i);
- }
- define('PATH_INFO', true);
- url_parse();
- header("Content-type:text/html;charset=utf-8");
- echo "urlencode:";
- echo urlencode($_GET['class']) . ',' . $_GET['news'];
- echo '<br />urldecode:';
- echo urldecode($_GET['class']) . ',' . $_GET['news'];
- echo '<br />';
- echo '<img src="/Public/Images/display.png" />';
- echo '<br />';
- echo '<a href="http://localhost/test.php/class/淡水/news/100.html">my link1</a>';
- echo '<hr />';
- echo '<a href="http://localhost/test.php/class/' . urlencode('淡水') . '/news/100.html">my link2</a>';
- echo '<hr />';
- echo '<a href="http://localhost/test.php?class=淡水&news=100">my link3</a>';
- ?>
注意点:原文代码里html在htm后面,这样地址为xxx.html的时候,会有一个L没替换到。
直接输入:http://localhost/test.php?class=淡水&news=100 中文会有乱码,点击连接却不会。其它都正常。怪事?!!!


