【问题标题】:preg_replace special charspreg_replace 特殊字符
【发布时间】:2012-05-07 13:19:54
【问题描述】:

我尝试替换

http://site.com/13/new new (2011).html

http://site.com/13/new_new_2011.html

这是我的代码,

<a href="<?php echo $row->id;?>/<?=preg_replace('#[^\w\-]+#','_', strtolower($row->showTitle()))?>.html">

$row->showTitle() 是一个类似于 New New (2011)

的标题

但问题是因为我的链接现在看起来像这样

http://site.com/13/new_new_2011_.html

我需要从 url _.html 中删除最后一个“_”

【问题讨论】:

  • 您必须将 \s(空格)替换为 _ AND ( 替换为 ''

标签: php regex preg-replace


【解决方案1】:

试试这个:

 <a href="<?php echo $row->id;?>/
 <?=trim(preg_replace('#[^\w\-]+#','_', strtolower($row->showTitle())), '_')?>.html"> 

它将删除任何尾随下划线。

【讨论】:

    猜你喜欢
    • 2016-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-03
    • 1970-01-01
    • 2012-04-06
    • 1970-01-01
    • 2014-05-04
    相关资源
    最近更新 更多