开发者

php won't detect all spaces in a string

I've got a string that comes from a POST form where I want to replace all spaced with some other character.

Here's that I did:

$cdata = str_replace(" ","#",$cdata);

And I got this.

开发者_StackOverflow社区--- Contact-ID#=#148 [10274da8]#Sinhronizācija#=#private [1000137d]#Uzvārds#=#Zom [1000137c]#Vārds#=#Tana [1000130e]#Tālrunis#=#3333 [1000130e]#Mobilais#=#5555

As you can see, spaced before "[10..." are still there. Any ideas what could be the problem?


It's mostly likely because it's a newline character, \n. The first param of str_replace can be an array of characters to replace. Could also be a tab char. Or use preg_replace to replace all whitespace chars instead.

EDIT:

$chars_to_replace = array(" ", "\t","\n","\r","\o","\x0B");
$new_string = str_replace($chars_to_replace, "#", $cdata);


You need preg_replace here:

$cdata = preg_replace('/\s+/', '#', cdata);


GOT IT!!! HA-HA-HA! Being a good programmer I had used mysqli_real_escape_string on $_POST before assigning it to $cdata. Now I removed it and seems to work perfect! Thanks guys!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜