I have a string with “\u00a0”, and I need to replace it with “” str_replace fails

后端 未结 8 1214
南笙
南笙 2020-12-17 09:45

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

相关标签:
8条回答
  • 2020-12-17 09:55

    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 );
    
    0 讨论(0)
  • 2020-12-17 09:55

    A minor point: \u00a0 is actually a non-breaking space character, c.f. http://www.fileformat.info/info/unicode/char/a0/index.htm

    So it might be more correct to replace it with " "

    0 讨论(0)
  • 2020-12-17 10:09

    This did the trick for me:

    $str = preg_replace( "~\x{00a0}~siu", " ", $str );
    
    0 讨论(0)
  • 2020-12-17 10:13

    Works for me, when I copy/paste your code. Try replacing the double quotes in your str_replace() with single quotes, or escaping the backslash ("\\u00a0").

    0 讨论(0)
  • 2020-12-17 10:13

    I just had the same problem. Apparently PHP's json_encode will return null for any string with a 'non-breaking space' in it.

    The Solution is to replace this with a regular space:

    str_replace(chr(160),' ');
    

    I hope this helps somebody - it took me an hour to figure out.

    0 讨论(0)
  • 2020-12-17 10:14

    Try this:

    $str = str_replace("\u{00a0}", ' ', $str);
    
    0 讨论(0)
提交回复
热议问题