How to replace decoded Non-breakable space (nbsp)

时光怂恿深爱的人放手 提交于 2019-12-18 04:01:20

问题


Assuming I have a sting which is "a s d d" and htmlentities turns it into
"a s d d".

How to replace (using preg_replace) it without encoding it to entities?

I tried preg_replace('/[\xa0]/', '', $string);, but it's not working. I'm trying to remove those special characters from my string as I don't need them

What are possibilities beyond regexp?

Edit String I want to parse: http://pastebin.com/raw/7eNT9sZr
with function preg_replace('/[\r\n]+/', "[##]", $text)
for later implode("</p><p>", explode("[##]", $text))

My question is not exactly "how" to do this (since I could encode entities, remove entities i don't need and decode entities). But how to remove those with just str_replace or preg_replace.


回答1:


The problem is that you are specifying the non-breakable UTF-8 space in a wrong way. The proper code is 0xc2a0, you're specifying only the half of the character's code.

You can replace it using the simple (and fast) str_replace or using a more flexible regular expression, depending on your needs:

// faster solution
$regular_spaces = str_replace("\xc2\xa0", ' ', $original_string);

// more flexible solution
$regular_spaces = preg_replace('/\xc2\xa0/', ' ', $original_string);

Note that in case of str_replace, you have to use double quotes (") to enclose the search string because it doesn't understand raw character codes so it needs those codes to be converted into actual characters first. That's made automatically by PHP because strings enclosed in quotes are being processed and special sequences (e.g. newline character \n, character codes, etc.) are replaced before the string value is being used.

In contrast, the preg_replace function itself understands raw character codes so you don't need PHP to convert the codes into actual characters and you can use apostrophes (single quotes, ') to enclose the search string in this case.

Note how the UTF-8 character code is specified as two separate numbers.



来源:https://stackoverflow.com/questions/40724543/how-to-replace-decoded-non-breakable-space-nbsp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!