数据库字段中的空白不会被trim()删除
我在 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
尝试通过写出字符代码来检查每个“空白”是什么字符 - 它可能是不可见的字符类型,不会被修剪删除。
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.
尝试
try
不间断空格的 UTF-8 字符编码 Unicode (U+00A0) 是 2 字节序列
C2
A0
。我尝试使用 trim() 的第二个参数 但这并没有达到目的。使用示例:TRIM(text_field)
的 MySQL 替代品,也删除了 UTF 不间断空格,感谢 @RudolfRein 的评论:UTF-8 检查表:(
更多检查 此处)
确保您的 PHP 源代码编辑器位于
UTF-8 模式 无 BOM。或者在首选项中进行设置。
确保您的 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('内容类型:text/html; charset=utf-8');
或
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 totrim()
but that didn't do the trick. Example use:A MySQL replacement for
TRIM(text_field)
that also removes UTF no-break spaces, thanks to @RudolfRein's comment:UTF-8 checklist:
(more checks here)
Make sure your PHP source code editor is in
UTF-8 mode without BOM. Or set in the preferences.
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");
Make sure your HTTP server is set for UTF-8, e.g. for Apache:
AddDefaultCharset UTF-8
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" />
如果问题出在 UTF-8 NBSP 上,另一个简单的选择是:
If the problem is with UTF-8 NBSP, another simple option is:
最好的解决方案是结合已经提到的一些事情。
首先对有问题的字符串运行 ORD()。就我而言,我必须首先运行反向操作,因为我的问题字符位于字符串的末尾。
一旦发现有问题的字符,请运行“
最后,调用适当的”
这将从字符串的各个方面完全消除问题字符,并修剪掉前导(在我的情况下为尾随)字符。
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.
Once you discover the problematic char, run a
Finally, call a proper
This will completely eradicate the problem character from all aspects of the string, and trim off the leading (in my case trailing) character.
尝试在
text_field
上使用 MySQLORD()
函数来检查最左边字符的字符代码。它可以是看起来像空格的非空白字符。Try using the MySQL
ORD()
function on thetext_field
to check the character code of the left-most character. It can be a non-whitespace characters that appears like whitespace.您必须首先检测这些“空白”字符。如果它是一些 HTML 实体,例如
当然,没有修剪功能会有所帮助。
我建议像这样打印出来
,看看它说什么
。由于它的
A0
(或十进制 160)不间断空格字符,您可以先将其转换为序数空格:并且,ta-坝! -
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
and see what it says
Well as its
A0
(or 160 decimal) non-breaking space character, you can convert it to ordinal space first:and, ta-dam! -