这个的正则表达式是什么


What Regex for this?

我试着写一个好的正则表达式,但即使有文档,我也不知道如何写一个好的正则表达式。

我有很多字符串,我需要清除这些字符。

例如:

70%库顿/30%行

应该改成:

库顿

70%——30%——行

事实上:

  • /'#必须替换为-

  • 空格必须删除

  • 必须替换不带重音的重音字符

setlocale(LC_ALL, "en_US.UTF8");
$string = '70%COTON/ 30%LINé';
$string = iconv('UTF-8', 'ASCII//TRANSLIT', $string);
$string = preg_replace("#[^'w'%'s]#", "", $string);
$string = str_replace(' ', '-', $string);
$string = preg_replace('#(-){2,}#', ' ', $string);
echo strtoupper($string); // 70%COTON-30%LINE

我将使用iconv()作为重音:

$text = 'glāžšķūņu rūķīši';
$text = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $text);
echo $text; // outputs "glazskunu rukisi"

要做剩下的,我将添加strtoupper()来改变字母的大小写,str_replace()来摆脱空格,preg_replace()来将那些少数字符转换为-:

$text = 'glāžšķūņu rūķīši / '' # test';
$text = iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $text);
$text = strtoupper($text);
$text = str_replace(' ', '', $text);
$text = preg_replace('#[/''#'''']+#', '-', $text);
echo $text; // outputs "GLAZSKUNURUKISI-TEST"