dsk710351 2016-09-16 09:22
浏览 27
已采纳

在URL中通过mod_rewrite使用 - 并查询Mysql数据库

I have a problem. I am using mod_rewrite to rename my URLS

Everything is working correctly until I reformatted my URLS so that spaces include - instead of %20.

site.com/page.php?country=abc&region=d e f&city=gh i

site.com/abc/d-e-f/gh-i/

Currently I am doing this

if (isset($_GET['country'])) {$country = str_replace('-', ' ', $_GET['country']);}
if (isset($_GET['city'])) {$city = str_replace('-', ' ', $_GET['city']);}
if (isset($_GET['region'])) {$region = str_replace('-', ' ', $_GET['region']);}

I then convert back to normal when querying the database for example:

str_replace(' ', '-', $_GET['city'])

However this has created a problem as some cities use both a space AND a -. Saint-andre reunion island.

Initially I thought about running the original SQL query and if 0 results are returned, str_replacing spaces with - before running the query again. However this will only fix it if the city contains ONLY - and 0 space eg Saint-andre.

$sql2 = "SELECT `name`, FROM `list` WHERE country = '$regionCode[0]' AND admin1 = '$regionCode[1]' AND admin2 = '$regionCode[2]' AND city = '$city' ORDER BY 'moddate'";

What is the best way to handle this? Should I use double dashes instead and handle this differently? EG Saint--andre-reunion-island or is there a better way?

Thanks

  • 写回答

1条回答 默认 最新

  • douduan5073 2016-09-16 18:28
    关注

    Decided to go with the double dash option and created a function. I still welcome any other suggestions if there is a better option :)

    Code that I am using below.

       // URL Database conversion class
    function QueryConversion($url) {
    $url = str_replace('--', '*', $url);
    $url = str_replace('-', ' ', $url);
    $url = str_replace('*', '-', $url);
    return $url; 
    }
    
    // URL conversion class
    function URLconversion($url) {
    $url = str_replace('-', '--', $url);
    $url = str_replace(' ', '-', $url);
    return $url;
    }
    
    if (isset($_GET['country'])) {$country = QueryConversion($_GET['country']);}
    if (isset($_GET['region'])) {$region = QueryConversion($_GET['region']);}
    if (isset($_GET['city'])) {$city = QueryConversion($_GET['city']);}
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办