数据库字段中的空白不会被trim()删除

发布于 2024-10-21 19:23:45 字数 213 浏览 9 评论 0原文

我在 MySQL 的文本字段的段落开头有一些空格。

在 PHP 中使用 trim($var_text_field) 或在 MySQL 语句中使用 TRIM(text_field) 绝对不会执行任何操作。这个空白可能是什么以及如何通过代码删除它?

如果我进入数据库并将其退格,它就会正确保存。它只是没有通过trim() 函数删除。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

风和你 2024-10-28 19:23:46

尝试通过写出字符代码来检查每个“空白”是什么字符 - 它可能是不可见的字符类型,不会被修剪删除。
Trim 仅删除一些字符,例如空格、制表符、换行符、CR 和 NUL,但存在其他可能导致此问题的不可见字符。

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.

ι不睡觉的鱼゛ 2024-10-28 19:23:46

尝试

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

try

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

娇妻 2024-10-28 19:23:45
function UberTrim($s) {
    $s = preg_replace('/\xA0/u', ' ', $s);  // strips UTF-8 NBSP: "\xC2\xA0"
    $s = trim($s);
    return $s;
}

不间断空格的 UTF-8 字符编码 Unicode (U+00A0) 是 2 字节序列 C2 A0。我尝试使用 trim() 的第二个参数 但这并没有达到目的。使用示例:

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

TRIM(text_field) 的 MySQL 替代品,也删除了 UTF 不间断空格,感谢 @RudolfRein 的评论:

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

UTF-8 检查表:(

更多检查 此处)

  1. 确保您的 PHP 源代码编辑器位于 以无 BOM 的 UTF-8 编码 (Notepad++) UTF-8 模式 无 BOM。或者在首选项中进行设置。

  2. 确保您的 MySQL 客户端设置为 UTF-8 字符编码(更多信息此处此处),例如

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

  3. 确保您的 HTTP 服务器设置为 UTF-8,例如 Apache

    AddDefaultCharset UTF-8

  4. 确保浏览器需要 UTF-8。

    header('内容类型:text/html; charset=utf-8');

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" />

微凉徒眸意 2024-10-28 19:23:45

如果问题出在 UTF-8 NBSP 上,另一个简单的选择是:

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

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

REPLACE(the_field, UNHEX('C2A0'), ' ')
泅渡 2024-10-28 19:23:45

最好的解决方案是结合已经提到的一些事情。

首先对有问题的字符串运行 ORD()。就我而言,我必须首先运行反向操作,因为我的问题字符位于字符串的末尾。

ORD(REVERSE([col name])) 

一旦发现有问题的字符,请运行“

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

最后,调用适当的”

TRIM([col_name])

这将从字符串的各个方面完全消除问题字符,并修剪掉前导(在我的情况下为尾随)字符。

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.

或十年 2024-10-28 19:23:45

尝试在 text_field 上使用 MySQL ORD() 函数来检查最左边字符的字符代码。它可以是看起来像空格的非空白字符。

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.

累赘 2024-10-28 19:23:45

您必须首先检测这些“空白”字符。如果它是一些 HTML 实体,例如   当然,没有修剪功能会有所帮助。

我建议像这样打印出来

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-坝! -

string(4) " bla"
string(3) "bla"

you have to detect these "whitespace" characters first. if it's some HTML entity, like   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"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文