【问题标题】:Url replacing网址替换
【发布时间】:2009-07-05 02:24:08
【问题描述】:

我正在尝试制作一些不错的网址。

CREATE TABLE IF NOT EXISTS `games` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(200) DEFAULT NULL,
`about` text,
`release_date` int(10) NOT NULL DEFAULT '0'

php

$id = str_replace('_', ' ', $_GET['title']);
$query = mysql_query("SELECT * FROM games WHERE title = '$id'");
$row = mysql_fetch_assoc($query);

if (!mysql_num_rows($query)) exit('This game is not in the database.');

在替换字符方面需要一些帮助。

假设我的一个游戏的标题是 Day of Defeat: Source 我想通过以下方式访问它:Day_of_Defeat_Source。我该怎么做?删除冒号、- 和 & 以及所有这些都没有。现在是:Day_of_Defeat:_Source

但如果我将 : 替换为 _ 它看起来像:Day_of_Defeat__Source

我该如何解决这个问题?

对不起,我的英语很便宜,也许版主可以让这更容易理解。

【问题讨论】:

    标签: php sql


    【解决方案1】:

    所以将':' 替换为空字符串'' —— 谁说你必须在其中添加另一个下划线?

    【讨论】:

    • 它不会工作。 $id = str_replace('_', ' ', $_GET['title']); $id = str_replace(':', '', $_GET['title']);
    • 呃——这两个替换是什么,第二个忽略第一个?!第二个显然必须使用 $id 作为第三个参数!
    【解决方案2】:
    $bad = array('&', ':', '-'); //array of characters to remove
    $id = str_replace($bad, '', $_GET['title'];
    $id = str_replace('_', ' ', $id);
    ...continue with query...
    

    【讨论】:

      【解决方案3】:
      $addr = "Day of Defeat: Source";
      $search = array(" ", ":"); 
      $replace = array("_", ""); 
      $new_addr = str_replace($search, $replace, $addr);
      

      $search 中的每个值都将在 $addr 中搜索,如果找到,则将其替换为 $replace 中的相应值。

      因此在这种情况下,您将“”替换为“_”,并且将“:”替换为“”,因此您将获得 Day_of_Defeat_Source

      您可以向数组 $search 和 $replace 添加任何字符。

      【讨论】:

        【解决方案4】:

        您可以URL-encode 它,然后使用URL-decode 阅读它。

        “失败日:来源”将是“Day%20of%20Defeat%3A%20Source”。

        【讨论】:

          【解决方案5】:

          我建议无论你在替换角色时做出什么样的选择,你都应该将游戏的 URL 标题存储在数据库中。这将使您更容易通过 URL 查找游戏,因为您不必每次都撤消替换逻辑。它还允许您创建一个不错的 URL,而不必使替换过程可逆(可逆?)。

          消除了可逆性,您现在可以执行此操作,这会删除双下划线:

          $id = str_replace(array(" ", ":"), '_', $_GET['title']);
          $doubledUp = strpos($id, "__");
          while ($doubledUp !== false) {
              $id = str_replace("__", "_", $id);
              $doubledUp = strpos($id, "__");
          }
          

          【讨论】:

            【解决方案6】:

            试试这个..

            $string = "Day of Defeat: Source";
            $string = preg_replace("/[^a-zA-Z0-9 ]/", " ", $string); //remove all non-alphanumeric
            $string = preg_replace('/\s+/', ' ', $string); //more than one spaces to single space
            $string = preg_replace('/\s/', '_', $string); //space to an underscore
            echo $string;
            

            希望这会有所帮助。

            【讨论】:

              【解决方案7】:

              我很久以前为Kohana框架写了一个函数URL::title()

              /**
               * Convert a phrase to a URL-safe title. Note that non-ASCII characters
               * should be transliterated before using this function.
               *
               * @param   string  phrase to convert
               * @param   string  word separator (- or _)
               * @return  string
               */
              public static function title($title, $separator = '-')
              {
                  $separator = ($separator === '-') ? '-' : '_';
              
                  // Remove all characters that are not the separator, a-z, 0-9, or whitespace
                  $title = preg_replace('/[^'.$separator.'a-z0-9\s]+/', '', strtolower($title));
              
                  // Replace all separator characters and whitespace by a single separator
                  $title = preg_replace('/['.$separator.'\s]+/', $separator, $title);
              
                  // Trim separators from the beginning and end
                  return trim($title, $separator);
              }
              

              View source at Github

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 2019-08-18
                • 1970-01-01
                • 2018-04-24
                • 2019-05-26
                • 2018-03-21
                • 2019-11-17
                • 1970-01-01
                相关资源
                最近更新 更多