dongsou3041 2009-04-12 09:21
浏览 349
已采纳

正则表达式匹配除标点符号之外的任何UTF字符

I'm preparing a function in PHP to automatically convert a string to be used as a filename in a URL (*.html). Although ASCII should be use to be on the safe side, for SEO needs I need to allow the filename to be in any language but I don't want it to include punctuation other than a dash (-) and underscore (_), chars like *%$#@"' shouldn't be allowed.

Spaces should be converted to dashes.

I think that using Regex will be the easiest way, but I'm not sure it how to handle UTF8 strings.

My ASCII functions looks like this:

function convertToPath($string)
{
    $string = strtolower(trim($string));
    $string = preg_replace('/[^a-z0-9-]/', '-', $string);
    $string = preg_replace('/-+/', "-", $string);
    return $string;
}

Thanks,

Roy.

  • 写回答

2条回答 默认 最新

  • dozxos6346 2009-04-12 09:36
    关注

    I think that for SEO needs you should stick to ASCII characters in the URL.

    In theory, many more characters are allowed in URLs. In practice most systems only parse ASCII reliable.

    Also, many automagically-parse-the-link scripts choke on non-ASCII characters. So allowing URLs with non-ASCII characters in your URLs drastically reduces the change of your link showing up (correctly) in user generated content. (if you want an example of such a script, take a look at the stackoverflow script, it chokes on parenthesis for example)

    You could also take a look at: How to handle diacritics (accents) when rewriting ‘pretty URLs’

    The accepted solution there is to transiterate the non-ASCII characters:

    <?php
      $text = iconv('UTF-8', 'US-ASCII//TRANSLIT', $text);
    ?>
    

    Hope this helps

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 怎么在stm32门禁成品上增加记录功能
  • ¥15 Source insight编写代码后使用CCS5.2版本import之后,代码跳到注释行里面
  • ¥50 NT4.0系统 STOP:0X0000007B
  • ¥15 想问一下stata17中这段代码哪里有问题呀
  • ¥15 flink cdc无法实时同步mysql数据
  • ¥100 有人会搭建GPT-J-6B框架吗?有偿
  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app