preg_replace特殊的字符


preg_replace special chars

我尝试替换

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

我需要从网址中删除最后一个"_.html

试试这个:

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

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