Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中将标题转换为url兼容模式的最佳方法?_Php_Url - Fatal编程技术网

在PHP中将标题转换为url兼容模式的最佳方法?

在PHP中将标题转换为url兼容模式的最佳方法?,php,url,Php,Url,上述url将报告400错误请求 如何将此类标题转换为用户友好的好的请求?您可以使用该功能参见此处的第一个答案: 您可以使用urlencode或rawurlencode。。。例如,维基百科就是这样做的。请参阅此链接: 这是%=%25的php编码,您可能需要使用“slug”来代替。不使用逐字标题作为URL,而是使用连字号替换所有非字母数字字符,然后删除重复的连字号。如果你想要额外的学分,你也可以去掉 因此,“1-低至10%首付,免费高尔夫会员!!!”变成: as-low-as-10-首付-免费-黄

上述url将报告
400错误请求

如何将此类标题转换为用户友好的好的请求?

您可以使用该功能

参见此处的第一个答案:


您可以使用urlencode或rawurlencode。。。例如,维基百科就是这样做的。请参阅此链接:

这是%=%25的php编码,您可能需要使用“slug”来代替。不使用逐字标题作为URL,而是使用连字号替换所有非字母数字字符,然后删除重复的连字号。如果你想要额外的学分,你也可以去掉

因此,“1-低至10%首付,免费高尔夫会员!!!”变成:

as-low-as-10-首付-免费-黄金-会员资格

大概是这样的:

function Slug($string)
{
    return strtolower(trim(preg_replace('~[^0-9a-z]+~i', '-', html_entity_decode(preg_replace('~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i', '$1', htmlentities($string, ENT_QUOTES, 'UTF-8')), ENT_QUOTES, 'UTF-8')), '-'));
}

$user = 'Alix Axel';
echo Slug($user); // alix-axel

$user = 'Álix Ãxel';
echo Slug($user); // alix-axel

$user = 'Álix----_Ãxel!?!?';
echo Slug($user); // alix-axel

您可能可以使用更长的regexp来缩短它,但它非常简单。好处是,在数据库上运行查询以匹配标题之前,您可以使用相同的函数验证查询参数,这样就不会有人在数据库中插入愚蠢的东西。

我只是用一个有用的slug函数创建了一个要点:

您可以使用它将标题转换为seo友好的url

function sluggify($url)
{
    # Prep string with some basic normalization
    $url = strtolower($url);
    $url = strip_tags($url);
    $url = stripslashes($url);
    $url = html_entity_decode($url);

    # Remove quotes (can't, etc.)
    $url = str_replace('\'', '', $url);

    # Replace non-alpha numeric with hyphens
    $match = '/[^a-z0-9]+/';
    $replace = '-';
    $url = preg_replace($match, $replace, $url);

    $url = trim($url, '-');

    return $url;
}

要简化,只需填写变量列表
$change\u To
$To\u change

<?php
class SanitizeUrl {

    public static function slug($string, $space="-") {
        $string = utf8_encode($string);
        if (function_exists('iconv')) {
            $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
        }

        $string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
        $string = trim(preg_replace("/\\s+/", " ", $string));
        $string = strtolower($string);
        $string = str_replace(" ", $space, $string);

        return $string;
    }
}

$title = 'Thi is a test string with some "strange" chars ò à ù...';
echo SanitizeUrl::slug($title);
//this will output:
//thi-is-a-test-string-with-some-strange-chars-o-a-u

@user198729:您能给出一个输入/预期输出的示例吗?这样,当字符串中有多个空格时,您将有多个“-”。
<?php
class SanitizeUrl {

    public static function slug($string, $space="-") {
        $string = utf8_encode($string);
        if (function_exists('iconv')) {
            $string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
        }

        $string = preg_replace("/[^a-zA-Z0-9 \-]/", "", $string);
        $string = trim(preg_replace("/\\s+/", " ", $string));
        $string = strtolower($string);
        $string = str_replace(" ", $space, $string);

        return $string;
    }
}

$title = 'Thi is a test string with some "strange" chars ò à ù...';
echo SanitizeUrl::slug($title);
//this will output:
//thi-is-a-test-string-with-some-strange-chars-o-a-u
<?php

// Just full the array list to make replacement complete
// In this space will change to _, à to just a



$to_change = [
    ' ', 'à', 'à', 'â','é', 'è', 'ê', 'ç', 'ù', 'ô', 'ö' // and so on
];

$change_to = [
    '_', 'a', 'a', 'a', 'e', 'e', 'e','c', 'u', 'o', 'o' // and so on
];

$texts = 'This is my slug in êlàb élaboré par';

$page_id = str_replace($to_change, $change_to, $texts);