简体   繁体   English

UTF-8 PHP将不可打印字符“空格”替换为不可打印字符“换行符”

[英]UTF-8 PHP replace non printable character “space” with non printable character “line break”

I have a string in UTF-8 with non-printable space which I need to replace with non-printable linebreak 我在UTF-8有一个带有non-printable space的字符串,需要将其替换为non-printable linebreak

something like this str_replace('&nbsp;','<br />',$string); 像这样的str_replace('&nbsp;','<br />',$string); but with non-printable characters . 但带有non-printable characters

It literally works if you type in the specific character between the quotes: 如果您在引号之间输入特定字符,则实际上可以使用:

str_replace(' ', '', $string)
             ^   ^^
        put characters here

Since this can arguably be a bit hard to type and/or make the source code less than obvious, you can write those string literals in their byte notation. 由于这可能很难键入和/或使源代码变得不那么明显,因此可以将这些字符串文字用其字节表示法编写。 Just figure out what specific character you're talking about and what bytes it's encoded in: 只需弄清楚您在说什么特定字符以及该字符编码的字节数即可:

str_replace("\xE2\x80\xAF", "\x0A", $string)

This is replacing a ZERO-WIDTH SPACE (UTF-8 encoding E2 80 AF ) with a regular line feed ( 0A ). 这将用常规换行符( 0A )替换零宽度空间(UTF-8编码E2 80 AF )。 Look it up in your Unicode table of choice. 在您选择的Unicode表中查找。 Possibly inspect your existing string using echo bin2hex($string) to figure out what bytes it contains. 可以使用echo bin2hex($string)检查您现有的字符串,以找出其中包含的字节。

How about: str_replace('&nbsp;' , PHP_EOL , $string); 怎么样: str_replace('&nbsp;' , PHP_EOL , $string);

Or even better: str_replace( array( '&nbsp;', ' ' ) , PHP_EOL , $string); 甚至更好: str_replace( array( '&nbsp;', ' ' ) , PHP_EOL , $string);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM