如何将字符串编码为这样的格式?

时间:2019-07-17 15:29:59

标签: php encode

我需要像这样编码一个字符串:

“克里斯·布朗-没有指导(壮志凌云)”

对此:“ chrisbrowndrake_noguidance”。

我做了一个函数来做到这一点,但这是大型且“复杂”的方式。 有没有一种方法可以使此操作更简单?

这是我已经做的功能:

function RFMCONVERT($value){
//SET
$rfmconv = $value;
//REMOVE
$rfmconv = str_replace(' - ', '_', $rfmconv);
$rfmconv = str_replace(' ', '', $rfmconv);
$rfmconv = preg_replace ('#\(.*?\)#m' , '' , $rfmconv);
$rfmconv = str_replace('&', '', $rfmconv);
$rfmconv = str_replace("À", "A", $rfmconv);
$rfmconv = str_replace("à", "a", $rfmconv);
$rfmconv = str_replace("Á", "A", $rfmconv);
$rfmconv = str_replace("á", "a", $rfmconv);
$rfmconv = str_replace("Â", "A", $rfmconv);
$rfmconv = str_replace("â", "a", $rfmconv);
$rfmconv = str_replace("Ã", "A", $rfmconv);
$rfmconv = str_replace("ã", "a", $rfmconv);
$rfmconv = str_replace("Ä", "A", $rfmconv);
$rfmconv = str_replace("ä", "a", $rfmconv);
$rfmconv = str_replace("Å", "A", $rfmconv);
$rfmconv = str_replace("å", "a", $rfmconv);
$rfmconv = str_replace("Æ", "Ae", $rfmconv);
$rfmconv = str_replace("æ", "ae", $rfmconv);
$rfmconv = str_replace("Ç", "C", $rfmconv);
$rfmconv = str_replace("ç", "c", $rfmconv);
$rfmconv = str_replace("È", "E", $rfmconv);
$rfmconv = str_replace("è", "e", $rfmconv);
$rfmconv = str_replace("É", "E", $rfmconv);
$rfmconv = str_replace("é", "e", $rfmconv);
$rfmconv = str_replace("Ê", "E", $rfmconv);
$rfmconv = str_replace("ê", "e", $rfmconv);
$rfmconv = str_replace("Ë", "E", $rfmconv);
$rfmconv = str_replace("ë", "e", $rfmconv);
$rfmconv = str_replace("Ì", "I", $rfmconv);
$rfmconv = str_replace("ì", "i", $rfmconv);
$rfmconv = str_replace("Í", "I", $rfmconv);
$rfmconv = str_replace("í", "i", $rfmconv);
$rfmconv = str_replace("Î", "I", $rfmconv);
$rfmconv = str_replace("î", "i", $rfmconv);
$rfmconv = str_replace("Ï", "I", $rfmconv);
$rfmconv = str_replace("ï", "i", $rfmconv);
$rfmconv = str_replace("Ñ", "N", $rfmconv);
$rfmconv = str_replace("ñ", "n", $rfmconv);
$rfmconv = str_replace("Ò", "O", $rfmconv);
$rfmconv = str_replace("ò", "o", $rfmconv);
$rfmconv = str_replace("Ó", "O", $rfmconv);
$rfmconv = str_replace("ó", "o", $rfmconv);
$rfmconv = str_replace("Ô", "O", $rfmconv);
$rfmconv = str_replace("ô", "o", $rfmconv);
$rfmconv = str_replace("Õ", "O", $rfmconv);
$rfmconv = str_replace("õ", "o", $rfmconv);
$rfmconv = str_replace("Ö", "Oe", $rfmconv);
$rfmconv = str_replace("ö", "oe", $rfmconv);
$rfmconv = str_replace("Ø", "Oe", $rfmconv);
$rfmconv = str_replace("ø", "oe", $rfmconv);
$rfmconv = str_replace("Ù", "U", $rfmconv);
$rfmconv = str_replace("ù", "u", $rfmconv);
$rfmconv = str_replace("Ú", "U", $rfmconv);
$rfmconv = str_replace("ú", "u", $rfmconv);
$rfmconv = str_replace("Û", "U", $rfmconv);
$rfmconv = str_replace("û", "u", $rfmconv);
$rfmconv = str_replace("Ü", "Ue", $rfmconv);
$rfmconv = str_replace("ü", "ue", $rfmconv);
$rfmconv = str_replace("Y´", "Y", $rfmconv);
$rfmconv = str_replace("y´", "y", $rfmconv);
$rfmconv = str_replace("ß", "ss", $rfmconv);
for ($i = 0; $i < 48; $i++)
    $rfmconv = str_replace(chr ($i), "", $rfmconv);
for ($i = 58; $i < 65; $i++)
    $rfmconv = str_replace(chr ($i), "", $rfmconv);
for ($i = 91; $i < 95; $i++)
    $rfmconv = str_replace(chr ($i), "", $rfmconv);
for ($i = 96; $i < 97; $i++)
    $rfmconv = str_replace(chr ($i), "", $rfmconv);
for ($i = 123; $i < 256; $i++)
    $rfmconv = str_replace(chr ($i), "", $rfmconv);
//LOWERCASE
$rfmconv = strtolower($rfmconv);
//RESULT
$result = $rfmconv;
//RETURN
return $result;
}

我认为这可以做得更好,更简单。

1 个答案:

答案 0 :(得分:0)

考虑使用已建立的软件包(例如cocur/slugify

),而不是尝试使用自己的本地特定的触发逻辑

然后您可以使用以下代码:

$string = 'CHRIS BROWN - NO GUIDANCE (FEAT. DRAKE)';
$string = str_replace(' - ', '_', $string);
$string = str_replace(' ', '', $string);
$string = preg_replace ('#\(.*?\)#m' , '' , $string);
echo $slugify->slugify($string, '_'); // chrisbrown_noguidance