GG.*_*GG. 17 php string seo friendly-url slug
来自维基百科:
甲蛞蝓是识别使用人类可读关键字的页面的URL的一部分.
为了使用户更容易键入URL,通常也会删除或替换特殊字符.例如,重音字符通常被英文字母中的字母取代; 标点符号通常被删除; 和空格(必须编码为%20或+)由短划线( - )或下划线(_)代替,这些更美观.
我开发了一个照片共享网站,用户可以在其上传,分享和查看照片.
所有页面都是自动生成的,没有我对标题的控制.因为照片的标题或用户的名称可能包含重音字符或空格,我需要一个功能来自动创建slugs并保持可读的URL.
我创建了以下函数来替换重音字符(èçëçî),删除标点符号和错误字符(#@&〜^!)并以破折号转换空格.
php:
function sluggable($str) {
$before = array(
'àáâãäåòóôõöøèéêëðçìíîïùúûüñšž',
'/[^a-z0-9\s]/',
array('/\s/', '/--+/', '/---+/')
);
$after = array(
'aaaaaaooooooeeeeeciiiiuuuunsz',
'',
'-'
);
$str = strtolower($str);
$str = strtr($str, $before[0], $after[0]);
$str = preg_replace($before[1], $after[1], $str);
$str = trim($str);
$str = preg_replace($before[2], $after[2], $str);
return $str;
}
Run Code Online (Sandbox Code Playgroud)
Nat*_*xet 30
我喜欢谷歌代码解决方案中的php-slugs代码.但是如果你想要一个适用于UTF-8的更简单的:
function format_uri( $string, $separator = '-' )
{
$accents_regex = '~&([a-z]{1,2})(?:acute|cedil|circ|grave|lig|orn|ring|slash|th|tilde|uml);~i';
$special_cases = array( '&' => 'and', "'" => '');
$string = mb_strtolower( trim( $string ), 'UTF-8' );
$string = str_replace( array_keys($special_cases), array_values( $special_cases), $string );
$string = preg_replace( $accents_regex, '$1', htmlentities( $string, ENT_QUOTES, 'UTF-8' ) );
$string = preg_replace("/[^a-z0-9]/u", "$separator", $string);
$string = preg_replace("/[$separator]+/u", "$separator", $string);
return $string;
}
Run Code Online (Sandbox Code Playgroud)
所以
echo format_uri("#@&~^!âèêëçî");
Run Code Online (Sandbox Code Playgroud)
输出
-and-aeeeci
Run Code Online (Sandbox Code Playgroud)
如果您发现一些错误,请评论
有些人在google.com上链接到"php-slugs",但看起来他们的页面现在有点麻烦了,所以在这里,如果有人需要它:
// source: https://code.google.com/archive/p/php-slugs/
function my_str_split($string)
{
$slen=strlen($string);
for($i=0; $i<$slen; $i++)
{
$sArray[$i]=$string{$i};
}
return $sArray;
}
function noDiacritics($string)
{
//cyrylic transcription
$cyrylicFrom = array('?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?', '?');
$cyrylicTo = array('A', 'B', 'W', 'G', 'D', 'Ie', 'Io', 'Z', 'Z', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'R', 'S', 'T', 'U', 'F', 'Ch', 'C', 'Tch', 'Sh', 'Shtch', '', 'Y', '', 'E', 'Iu', 'Ia', 'a', 'b', 'w', 'g', 'd', 'ie', 'io', 'z', 'z', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'r', 's', 't', 'u', 'f', 'ch', 'c', 'tch', 'sh', 'shtch', '', 'y', '', 'e', 'iu', 'ia');
$from = array("Á", "À", "Â", "Ä", "?", "?", "Ã", "Å", "?", "Æ", "?", "?", "?", "?", "Ç", "?", "?", "Ð", "É", "È", "?", "Ê", "Ë", "?", "?", "?", "?", "?", "?", "?", "?", "á", "à", "â", "ä", "?", "?", "ã", "å", "?", "æ", "?", "?", "?", "?", "ç", "?", "?", "ð", "é", "è", "?", "ê", "ë", "?", "?", "?", "?", "?", "?", "?", "?", "?", "?", "I", "Í", "Ì", "?", "Î", "Ï", "?", "?", "?", "?", "?", "?", "?", "?", "?", "Ñ", "?", "Ó", "Ò", "Ô", "Ö", "Õ", "?", "Ø", "?", "Œ", "?", "?", "?", "í", "ì", "i", "î", "ï", "?", "?", "?", "?", "?", "?", "?", "?", "?", "ñ", "?", "ó", "ò", "ô", "ö", "õ", "?", "ø", "?", "œ", "?", "?", "?", "?", "Š", "?", "?", "?", "Þ", "Ú", "Ù", "Û", "Ü", "?", "?", "?", "?", "?", "?", "?", "Ý", "?", "Ÿ", "?", "?", "Ž", "?", "?", "?", "?", "š", "?", "ß", "?", "?", "þ", "ú", "ù", "û", "ü", "?", "?", "?", "?", "?", "?", "?", "ý", "?", "ÿ", "?", "?", "ž");
$to = array("A", "A", "A", "AE", "A", "A", "A", "A", "A", "AE", "C", "C", "C", "C", "C", "D", "D", "D", "E", "E", "E", "E", "E", "E", "E", "E", "G", "G", "G", "G", "G", "a", "a", "a", "ae", "ae", "a", "a", "a", "a", "ae", "c", "c", "c", "c", "c", "d", "d", "d", "e", "e", "e", "e", "e", "e", "e", "e", "g", "g", "g", "g", "g", "H", "H", "I", "I", "I", "I", "I", "I", "I", "I", "IJ", "J", "K", "L", "L", "N", "N", "N", "N", "O", "O", "O", "OE", "O", "O", "O", "O", "CE", "h", "h", "i", "i", "i", "i", "i", "i", "i", "i", "ij", "j", "k", "l", "l", "n", "n", "n", "n", "o", "o", "o", "oe", "o", "o", "o", "o", "o", "R", "R", "S", "S", "S", "S", "T", "T", "T", "U", "U", "U", "UE", "U", "U", "U", "U", "U", "U", "W", "Y", "Y", "Y", "Z", "Z", "Z", "r", "r", "s", "s", "s", "s", "ss", "t", "t", "b", "u", "u", "u", "ue", "u", "u", "u", "u", "u", "u", "w", "y", "y", "y", "z", "z", "z");
$from = array_merge($from, $cyrylicFrom);
$to = array_merge($to, $cyrylicTo);
$newstring=str_replace($from, $to, $string);
return $newstring;
}
function makeSlugs($string, $maxlen=0)
{
$newStringTab=array();
$string=strtolower(noDiacritics($string));
if(function_exists('str_split'))
{
$stringTab=str_split($string);
}
else
{
$stringTab=my_str_split($string);
}
$numbers=array("0","1","2","3","4","5","6","7","8","9","-");
//$numbers=array("0","1","2","3","4","5","6","7","8","9");
foreach($stringTab as $letter)
{
if(in_array($letter, range("a", "z")) || in_array($letter, $numbers))
{
$newStringTab[]=$letter;
}
elseif($letter==" ")
{
$newStringTab[]="-";
}
}
if(count($newStringTab))
{
$newString=implode($newStringTab);
if($maxlen>0)
{
$newString=substr($newString, 0, $maxlen);
}
$newString = removeDuplicates('--', '-', $newString);
}
else
{
$newString='';
}
return $newString;
}
function checkSlug($sSlug)
{
if(preg_match("/^[a-zA-Z0-9]+[a-zA-Z0-9\-]*$/", $sSlug) == 1)
{
return true;
}
return false;
}
function removeDuplicates($sSearch, $sReplace, $sSubject)
{
$i=0;
do{
$sSubject=str_replace($sSearch, $sReplace, $sSubject);
$pos=strpos($sSubject, $sSearch);
$i++;
if($i>100)
{
die('removeDuplicates() loop error');
}
}while($pos!==false);
return $sSubject;
}
Run Code Online (Sandbox Code Playgroud)
小智 6
setlocale(LC_ALL, 'en_US.UTF8');
function slugify($text)
{
// replace non letter or digits by -
$text = preg_replace('~[^\\pL\d]+~u', '-', $text);
// trim
$text = trim($text, '-');
// transliterate
$text = iconv('utf-8', 'us-ascii//TRANSLIT', $text);
// lowercase
$text = strtolower($text);
// remove unwanted characters
$text = preg_replace('~[^-\w]+~', '', $text);
if (empty($text))
{
return 'n-a';
}
return $text;
}
$slug = slugify($var);
Run Code Online (Sandbox Code Playgroud)
小智 3
我在网上找到了这个,完全按照你的要求,但保留了盒子。
\n\nfunction sluggable($p) {\n $ts = array("/[\xc3\x80-\xc3\x85]/","/\xc3\x86/","/\xc3\x87/","/[\xc3\x88-\xc3\x8b]/","/[\xc3\x8c-\xc3\x8f]/","/\xc3\x90/","/\xc3\x91/","/[\xc3\x92-\xc3\x96\xc3\x98]/","/\xc3\x97/","/[\xc3\x99-\xc3\x9c]/","/[\xc3\x9d-\xc3\x9f]/","/[\xc3\xa0-\xc3\xa5]/","/\xc3\xa6/","/\xc3\xa7/","/[\xc3\xa8-\xc3\xab]/","/[\xc3\xac-\xc3\xaf]/","/\xc3\xb0/","/\xc3\xb1/","/[\xc3\xb2-\xc3\xb6\xc3\xb8]/","/\xc3\xb7/","/[\xc3\xb9-\xc3\xbc]/","/[\xc3\xbd-\xc3\xbf]/");\n $tn = array("A","AE","C","E","I","D","N","O","X","U","Y","a","ae","c","e","i","d","n","o","x","u","y");\n return preg_replace($ts,$tn, $p);\n}\nRun Code Online (Sandbox Code Playgroud)\n\n\n
| 归档时间: |
|
| 查看次数: |
47756 次 |
| 最近记录: |