4

I have some whitespace at the begining of a paragraph in a text field in MySQL.

Using trim($var_text_field) in PHP or TRIM(text_field) in MySQL statements does absolutely nothing. What could this whitespace be and how do I remove it by code?

If I go into the database and backspace it out, it saves properly. It's just not being removed via the trim() functions.

5
  • what datatype is the columns, VARCHAR or CHAR?
    – krock
    Mar 14, 2011 at 7:44
  • some code would really help in answering.
    – Nishant
    Mar 14, 2011 at 7:44
  • Is this plain text or are you using different encoding?
    – JohnP
    Mar 14, 2011 at 7:45
  • @krok: the field is text @JohnP: it's being stored as UTF8_general
    – kylex
    Mar 14, 2011 at 15:59
  • it's non-breaking space character. see updated answer Mar 14, 2011 at 21:35

7 Answers 7

13
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 a no-break space, Unicode (U+00A0), is the 2-byte sequence C2 A0. I tried to make use of the second parameter to trim() but that didn't do the trick. Example use:

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

A MySQL replacement for TRIM(text_field) that also removes UTF no-break spaces, thanks to @RudolfRein's comment:

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

UTF-8 checklist:

(more checks here)

  1. Make sure your PHP source code editor is in Encode in UTF-8 without BOM (Notepad++) UTF-8 mode without BOM. Or set in the preferences.

  2. Make sure your MySQL client is set for UTF-8 character encoding (more here and here), e.g.

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

  3. Make sure your HTTP server is set for UTF-8, e.g. for Apache:

    AddDefaultCharset UTF-8

  4. Make sure the browser expects UTF-8.

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

    or

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

2
  • Thanks to @tchrist suggestion here about the 'u' flag in preg_replace().
    – Bob Stein
    Mar 28, 2013 at 1:18
  • YESS thank you !!! my sql data has stange spaces in it... I solved thanks to your \xc2\xa0 like this: WHERE REPLACE(rowname, '\xc2\xa0', ' ') LIKE '$searchstring%'
    – Jaxx0rr
    Jul 24, 2014 at 16:25
7

If the problem is with UTF-8 NBSP, another simple option is:

REPLACE(the_field, UNHEX('C2A0'), ' ')
3
  • oh! simple and perfect
    – Natxet
    Oct 18, 2018 at 11:43
  • Just as a reference for beginners, the full query is; UPDATE table_name SET the_field = REPLACE(the_field, UNHEX('C2A0'), ' ')
    – Alvin Moyo
    Dec 22, 2020 at 13:11
  • what a great trick! I started to have an headache here. TY!
    – SuN
    Apr 27, 2022 at 13:36
3

The best solution is a combination of a few things mentioned to you already.

First run ORD() on the string in question. In my case I had to run a reverse first because my problem character was at the end of the string.

ORD(REVERSE([col name])) 

Once you discover the problematic char, run a

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

Finally, call a proper

TRIM([col_name])

This will completely eradicate the problem character from all aspects of the string, and trim off the leading (in my case trailing) character.

2

Try using the MySQL ORD() function on the text_field to check the character code of the left-most character. It can be a non-whitespace characters that appears like whitespace.

4
  • This outputs 32, what does that mean?
    – kylex
    Mar 14, 2011 at 17:17
  • 32 is the code of the normal space character, which should be removed by TRIM() MySQL function. Try ORD(TRIM(text_field)).
    – Xint0
    Mar 15, 2011 at 2:34
  • This helped me, my non-breaking spaces were returning code 49824. Here is what I used to track entries and do a manual cleanup SELECT * FROM table WHERE (ord(substr(text_field,-1))) > 255 or (ord(substr(text_field,1,1))) > 255'
    – Benjamin
    Feb 11, 2013 at 21:08
  • 1
    @Benjamin, 49824 decimal = C2A0 hexadecimal. This is the 2-character UTF-8 code for NO-BREAK SPACE, Unicode (U+00A0). See my answer below.
    – Bob Stein
    Mar 28, 2013 at 0:45
1

you have to detect these "whitespace" characters first. if it's some HTML entity, like &nbsp; no trimming function would help, of course.

I'd suggest to print it out like this

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

and see what it says

Well as its A0 (or 160 decimal) non-breaking space character, you can convert it to ordinal space first:

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

and, ta-dam! -

string(4) " bla"
string(3) "bla"
3
  • It results with: %A0 Not sure what that translates to though.
    – kylex
    Mar 14, 2011 at 17:13
  • @kylex what does it say: echo urlencode(trim($row['field'])); Mar 14, 2011 at 18:34
  • It seems like the only thing I can do is substr($row['field'], 2), it's just a lot of info to process that way.
    – kylex
    Mar 14, 2011 at 21:28
0

Try to check what character each "whitespace" is by writing the charactercode out - It might be a non-visible charactertype that isn't removed by trim. Trim only removes a few characters such as whitespace, tab, newline, CR and NUL but there exist other non-visible characters that might cause this problem.

-1

try


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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.