How to replace decoded Non-breakable space (nbsp)

2019-04-05 17:51发布

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条回答
女痞
2楼-- · 2019-04-05 18:16

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.

查看更多
登录 后发表回答