dongqindan4406 2014-05-04 12:07
浏览 151

适用于所有语言的PHP slug函数

I'm wondering if there is a simple function/code that can take care of creating a slug from a given string.

I'm working on a multilingual website (English, Spanish, and Arabic) and I'm not sure how to handle that for Spanish and Arabic specifically.

I'm currently using the below code from CSS-Tricks but it doesn't work for UTF-8 text.

<?php
function create_slug($string){
   $slug=preg_replace('/[^A-Za-z0-9-]+/', '-', $string);
   return $slug;
}
echo create_slug('does this thing work or not');
//returns 'does-this-thing-work-or-not'
?>
  • 写回答

1条回答 默认 最新

  • dqblm40280 2017-10-31 17:39
    关注

    I created the library ausi/slug-generator for this purpose.

    It uses the PHP Intl extension, to translate between different scripts, which itself is based on the data from Unicode CLDR. This way it works with a wide set of languages.

    You can use it like so:

    <?php
    $generator = new SlugGenerator;
    
    $generator->generate('English language!');
    // Result: english-language
    
    $generator->generate('Idioma español!');
    // Result: idioma-espanol
    
    $generator->generate('لغة عربية');
    // Result: lght-rbyt
    
    评论

报告相同问题?

悬赏问题

  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测