str_replace没有删除空白


str_replace is not removing white spaces

我正试图通过'-'更改任何''

我是这样做的:

 $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可能要对它做点什么吗?

urlencode($tagname);会将空白变成%20,这样字符串就会变成

A%20tag%20with%20spaces

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