str_replace不会删除空格

时间:2012-02-01 17:30:50

标签: php str-replace

我想改变任何''by a' - '

我这样做:

 $tagname = $taginfo[1];   /* something like $tagname = 'A tag with spaces' */
 $tagurl = urlencode($tagname);
 $tagsize = 9 + intval($numtags)*2;
 $bla = 'function("'.str_replace(' ','-',$tagname).'")';

$ bla将用于附加到onclick属性

任何想法为什么结果

onclick="function('A tag with spaces')"

而不是

onclick="function('A-tag-with-spaces')" 

?可能htmlspecialchars必须用它做什么?

1 个答案:

答案 0 :(得分:2)

urlencode($tagname);会将您的空格变为%20,因此您的字符串将为

A%20tag%20with%20spaces

您可以将%20替换为-,也可以在编码前执行替换。