Spaces in the database field are not removed with trim ()

I have spaces at the beginning of a paragraph in a text box in MySQL.

Using trim($var_text_field)in PHP or TRIM(text_field)in MySQL operations does absolutely nothing. What is this gap and how to remove it using code?

If I log into the database and cancel it, it will be saved correctly. It simply is not removed through the trim () functions.

+3
source share
7 answers
function UberTrim($s) {
    $s = preg_replace('/\xA0/u', ' ', $s);  // strips UTF-8 NBSP: "\xC2\xA0"
    $s = trim($s);
    return $s;
}

The UTF-8 character encoding for non-breaking space, Unicode (U + 00A0), is a 2-byte sequence C2 A0 . I tried using the second parametertrim() , but that did not help. Usage example:

assert("abc" === UberTrim("  \r\n  \xc2\xa0  abc  \t \xc2\xa0   "));

MySQL TRIM(text_field), UTF, @RudolfRein:

TRIM(REPLACE(text_field, '\xc2\xa0', ' '))

UTF-8:

( )

  • , PHP Encode in UTF-8 without BOM (Notepad ++) UTF-8 . .

  • , MySQL UTF-8 ( ),

    $pdo = new PDO('mysql:host=...;dbname=...;charset=utf8',$userid,$password); $pdo->exec("SET CHARACTER SET utf8");

  • , HTTP UTF-8, . Apache:

    AddDefaultCharset UTF-8

  • , UTF-8.

    header('Content-Type: text/html; charset=utf-8');

    <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />

+12

MySQL ORD() text_field, . , .

+2

- .

ORD() . , .

ORD(REVERSE([col name])) 

char,

REPLACE([col_name], char([char_value_returned]), char(32))

,

TRIM([col_name])

( ).

+2

UTF-8 NBSP, :

REPLACE(the_field, UNHEX('C2A0'), ' ')
+2

"" . - HTML, &nbsp;, , .

echo urlenclde($row['field']);

,

A0 ( 160 ) , :

<pre><?php
$str = urldecode("%A0")."bla";
var_dump(trim($str));
$str = str_replace(chr(160)," ",$str);
$str = trim($str);
var_dump($str);

, ta-dam! -

string(4) " bla"
string(3) "bla"
+1

, "", charactercode. , . Trim , , , , CR NUL, , .

0

str_ireplace (array ("\ r", "\ n", "\ t"), $ var_text_field

-1
source

All Articles