Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace decoded Non-breakable space (nbsp)

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.

like image 804
Grzegorz Avatar asked Nov 21 '16 16:11

Grzegorz


People also ask

Why would you need a non-breaking space in your code?

So, the number of blank spaces remain the same as in the first example: Why would you need a non-breaking space in your code? Sometimes, HTML might break up words that are supposed to be together into another line – for example, initials, units, dates, amount of money, and more. The character entity prevents this from happening.

What is the non-breaking space in UTF-8 encoding?

The proper code for the non-breaking space in the UTF-8 encoding is 0xC2A0, it consists of two bytes - 0xC2 (194) and 0xA0 (160), so technically, you're specifying only the half of the character's code. A Bit of Theory Legacy character encodings were using the constant number of bits to encode every character in their set.

How many non-breaking spaces are there in HTML?

Instead, HTML provides the character entity for 2 non-breaking spaces, and for 4 non-breaking spaces. <div> <p> Lemurs     are primates found exclusively in the isolated island of Madagascar.

What is the use of &nbsp character in HTML?

The &nbsp character entity is used in HTML. It enables the presentation of many blank spaces. Below is how your code would look if you didn’t use the &nbsp character entity. To make the HTML clearer and simpler to read, like what we’re attempting to show, we include some CSS.


2 Answers

Problem Explanation

The reason why it's not working is that you are specifying the non-breaking space incorrectly.

The proper code for the non-breaking space in the UTF-8 encoding is 0xC2A0, it consists of two bytes - 0xC2 (194) and 0xA0 (160), so technically, you're specifying only the half of the character's code.

A Bit of Theory

Legacy character encodings were using the constant number of bits to encode every character in their set. For example, the original ASCII encoding was using 7 bits per character, extended ASCII 8 bits.

The UTF-8 encoding is so-called variable width character encoding, which means that the number of bits used to represent individual characters is variable, in the case of UTF-8, character codes consist of one up to four (8 bit) bytes (octets). In general, similarly to the Huffman coding, more frequently used characters have shorter codes while more rare characters have longer codes. That helps reduce the data size of the average text.

Solution

You can replace all occurences of the UTF-8 non-breaking space in text using a 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);

Notes

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

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

like image 93
David Ferenczy Rogožan Avatar answered Oct 11 '22 17:10

David Ferenczy Rogožan


Sanitize every type of white spaces.

preg_replace("/\s+/u", " ", $str);

https://stackoverflow.com/a/40264711/635364

FYI, PHP Sanitization filter_var() has no filter about these white spaces.

like image 15
Jehong Ahn Avatar answered Oct 11 '22 17:10

Jehong Ahn