dongmei8209 2009-02-12 03:41
浏览 21
已采纳

清理字符串以放置在URL中的最佳方法是什么,例如SO上的问题名称?

I'm looking to create a URL string like the one SO uses for the links to the questions. I am not looking at rewriting the url (mod_rewrite). I am looking at generating the link on the page.

Example: The question name is:

Is it better to use ob_get_contents() or $text .= ‘test’;

The URL ends up being:

http://stackoverflow.com/questions/292068/is-it-better-to-use-obgetcontents-or-text-test

The part I'm interested in is:

is-it-better-to-use-obgetcontents-or-text-test

So basically I'm looking to clean out anything that is not alphanumeric while still keeping the URL readable. I have the following created, but I'm not sure if it's the best way or if it covers all the possibilities:

$str = urlencode(
    strtolower(
    str_replace('--', '-', 
    preg_replace(array('/[^a-z0-9 ]/i', '/[^a-z0-9]/i'), array('', '-'), 
    trim($urlPart)))));

So basically:

  1. trim
  2. replace any non alphanumeric plus the space with nothing
  3. then replace everything not alphanumeric with a dash
  4. replace -- with -.
  5. strtolower()
  6. urlencode() -- probably not needed, but just for good measure.
  • 写回答

1条回答 默认 最新

  • dongyou7739 2009-02-12 08:30
    关注

    As you pointed out already, urlencode() is not needed in this case and neither is trim(). If I understand correctly, step 4 is to avoid multiple dashes in a row, but it will not prevent more than two dashes. On the other hand, dashes connecting two words (like in "large-scale") will be removed by your solution while they seem to be preserved on SO.

    I'm not sure that this is really the best way to do it, but here's my suggestion:

    $str = strtolower( 
      preg_replace( array('/[^a-z0-9\- ]/i', '/[ \-]+/'), array('', '-'), 
      $urlPart ) );
    

    So:

    1. remove any character that is neither space, dash, nor alphanumeric
    2. replace any consecutive number of spaces or dashes with a single dash
    3. strtolower()
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看