有一种更简单的方法,使用
iconv-从用户说明中看来,这似乎是您想要做的:字符音译
// PHP.net User notes<?php $string = "ʿABBĀSĀBĀD"; echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT', $string); // output: [nothing, and you get a notice] echo iconv('UTF-8', 'ISO-8859-1//IGNORE', $string); // output: ABBSBD echo iconv('UTF-8', 'ISO-8859-1//TRANSLIT//IGNORE', $string); // output: ABBASABAD // Yay! That's what I wanted!?>对字符编码要 非常谨慎
,因此在流程的所有阶段(前端,表单提交,源文件的编码)都应保持相同的编码。PHP和格式中的默认编码为ISO-8859-1,而PHP
5.4之前的默认编码已更改为UTF8(最终!)。
您可以使用几个功能来获取想法。首先是来自CakePHP的inflector类
slug:
public static function slug($string, $replacement = '_') { $quotedReplacement = preg_quote($replacement, '/'); $merge = array( '/[^sp{Ll}p{Lm}p{Lo}p{Lt}p{Lu}p{Nd}]/mu' => ' ', '/\s+/' => $replacement, sprintf('/^[%s]+|[%s]+$/', $quotedReplacement, $quotedReplacement) => '', ); $map = self::$_transliteration + $merge; return preg_replace(array_keys($map), array_values($map), $string);}它取决于一个
self::$_transliteration数组,该数组与您在问题中所做的操作类似-
您可以在github上查看inflector的源代码。
另一个是我个人使用的功能,它来自此处。
function slugify($text,$strict = false) { $text = html_entity_depre($text, ENT_QUOTES, 'UTF-8'); // replace non letter or digits by - $text = preg_replace('~[^\pLd.]+~u', '-', $text); // trim $text = trim($text, '-'); setlocale(LC_CTYPE, 'en_GB.utf8'); // transliterate if (function_exists('iconv')) { $text = iconv('utf-8', 'us-ascii//TRANSLIT', $text); } // lowercase $text = strtolower($text); // remove unwanted characters $text = preg_replace('~[^-w.]+~', '', $text); if (empty($text)) { return 'empty_$'; } if ($strict) { $text = str_replace(".", "_", $text); } return $text;}什么这些功能做的是音译,创造“
子弹从任意的文本输入,这是使Web应用程序时,在你的工具箱中一个非常非常有用的东西”。希望这可以帮助!



