Trim () does not catch AJAX published data value & nbsp;

I have a contenteditable element that onblur sends its value to the server.

Since it is contenteditable , with repeated spaces instead of white space,   , so the resulting HTML could be:

 Testing title   middle   entry     

What when extracted with the .text() element from the element looks like usually spaced (emphasizes spaces):

 Testing_title___middle___entry____ 

This is the .text() value that I am posting to my PHP server. There I look at the save procedure. Since I need a string trimmed, the base trim($title) did not cut.

And, I can't seem to hug him.

I also tried this. How to decode Unicode escape sequences such as "\ u00ed" for valid UTF-8 encoded characters? also did not help.

The Chrome Network Inspector shows what it sends on this encoded data:

 Testing+title+%C2%A0+middle+%C2%A0+entry+%C2%A0+%C2%A0 

implode(', ', unpack('C*', $title)) returns:

 84, 101, 115, 116, 105, 110, 103, // Testing 32, // space 116, 105, 116, 108, 101, // title 32, 194, 160, //   32, 109, 105, 100, 100, 108, 101, // middle 32, 194, 160, 32, 101, 110, 116, 114, 121, // entry 32, 194, 160, 32, 194, 160 

$title is ultimately just a $_POST['title'] .

How to trim all spaces from this line?


FINALLY, THE DECISION WAS:

 $title = preg_replace('/\s/iu', ' ', $title) 
+4
source share
1 answer

How about replacing all   spaces before trimming?

 $text= str_replace(' ', ' ', $text) 

Or, if you want to remain unicode safe, you can use mb_eregi_replace http://www.php.net/manual/en/function.mb-eregi-replace.php

EDIT - Second approach:

According to this answer ( fooobar.com/questions/535732 / ... )

194 160 is the UTF-8 encoding of the NO-BREAK SPACE code point (the same code as HTML calls).

So it really is not space, although it looks like one. (For example, you will see that this is not a word wrapper.) The regular expression match for \ s would match it, but there would be no simple comparison with a space.

Try using regexp to match it as suggested above?

+1
source

Source: https://habr.com/ru/post/1483553/


All Articles