【问题标题】:Looking to finish my url encoding in php希望在 php 中完成我的 url 编码
【发布时间】:2014-05-20 15:26:30
【问题描述】:

我有一个从另一个教程中使用的 slug 函数。

 public function createSlug($slug) {
        // Remove anything but letters, numbers, spaces, hypens
        // Remove spaces and duplicate dypens
        // Trim the left and right, removing any left over hypens
        
        $lettersNumbersSpacesHypens = '/[^\-\s\pN\pL]+/u';
        $spacesDuplicateHypens = '/[\-\s]+/';

        $slug = preg_replace($lettersNumbersSpacesHypens, '', mb_strtolower($slug, 'UTF-8'));   
        $slug = preg_replace($spacesDuplicateHypens, '-', $slug);
        $slug = trim($slug, '-');
        
        return $slug;
    }

效果很好。我有两个问题。

  1. 它给了我“amp”而不是删除“&”符号。不知道是不是应该这样。

    例如。

原始网址

http://www.mywebsite.com?category_id=1&category_name=hot & dogs 

使用 slug 函数的新网址

http://www.mywebsite.com?category_id=1&category_name=hot-amp-dogs

其次,如何将其解码回原始形式,以便在页面上回显它?与破折号相呼应看起来不正确。

【问题讨论】:

  • 简单的 -amp- 如您在链接中看到的那样。
  • 对于解码使用数据库中的两个字段用于 slug 和原始 url,因此需要从数据库中获取原始数据。所以不需要解码
  • 我明白了。我想我将不得不查找如何做到这一点。感谢您指出。

标签: php parameters urlencode symbols slug


【解决方案1】:

使用“htmlspecialchars_decode”。见以下修改功能:

function createSlug($slug) {
// Remove anything but letters, numbers, spaces, hypens
// Remove spaces and duplicate dypens
// Trim the left and right, removing any left over hypens
$slug   =   htmlspecialchars_decode($slug);

$lettersNumbersSpacesHypens = '/[^\-\s\pN\pL]+/u';
$spacesDuplicateHypens = '/[\-\s]+/';

$slug = preg_replace($lettersNumbersSpacesHypens, '', mb_strtolower($slug, 'UTF-8'));   
$slug = preg_replace($spacesDuplicateHypens, '-', $slug);
$slug = trim($slug, '-');

return $slug;
}

解码,同意Rakesh Sharma。使用数据库来管理它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多