Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
568 views
in Technique[技术] by (71.8m points)

php - I have a string with "u00a0", and I need to replace it with "" str_replace fails

I need to clean a string that comes (copy/pasted) from various Microsoft Office suite applications (Excel, Access, and Word), each with its own set of encoding.

I'm using json_encode for debugging purposes in order to being able to see every single encoded character.

I'm able to clean everything I found so far ( ) with str_replace, but with u00a0 I have no luck.

$string = '[email protected] u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0;[email protected]'; //this is the output from json_encode

$clean = str_replace("u00a0", "",$string);

returns:

[email protected] u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0 u00a0;[email protected]

That is exactly the same; it completely ignores u00a0.

Is there a way around this? Also, I'm feeling I'm reinventing the wheel, is there a function/class that completely strips EVERY possibile char of EVERY possible encoding?

____EDIT____

After the first two replies I need to clarify that my example DOES work, because it's the output from json_encode, not the actual string!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

By combining ord() with substr() on my string containing u00a0, I found the following curse to work:

$text = str_replace( chr( 194 ) . chr( 160 ), ' ', $text );

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...