php 如何从字符串中删除 NULL 字符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/642438/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 23:21:30  来源:igfitidea点击:

How can I remove the NULL character from string

php

提问by Benjamin Ortuzar

I have a PHP variable that contains a string which represents an XML structure. This string contains ilegal characters that dont let me build a new SimpleXMLElement object from the string. I dont have a way to ask the source of the content to modify their response, so I need to execute some cleaning on this string before I create a SimpleXMLElement object.

我有一个 PHP 变量,其中包含一个表示 XML 结构的字符串。此字符串包含不允许我从字符串构建新 SimpleXMLElement 对象的非法字符。我没有办法要求内容的来源修改他们的响应,所以我需要在创建 SimpleXMLElement 对象之前对这个字符串执行一些清理。

I believe the character causing the problem is a � (0x00 (00) HEX) character, and its located within one of the Text Nodes of this string XML.

我相信导致问题的字符是一个 (0x00 (00) HEX) 字符,它位于此字符串 XML 的文本节点之一内。

What is the best way to remove this character or other characters that could break the SimpleXMLElement object.

删除此字符或其他可能破坏 SimpleXMLElement 对象的字符的最佳方法是什么。

回答by Joey

$text = str_replace("
$text = trim($text);
", "", $text);

will replace all null characters in the $textstring. You can also supply arrays for the first two arguments, if you want to do multiple replacements.

将替换$text字符串中的所有空字符。如果要进行多次替换,还可以为前两个参数提供数组。

回答by bigtallbill

trim() will also remove null characters, from either end of the source string (but not within).

trim() 还将从源字符串的任一端(但不在其中)删除空字符。

$a = '[email protected]';
$b = 'bogus - at - example dot org';
$c = '([email protected])';

$sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) {
    echo "This (a) sanitized email address is considered valid.\n";
}

$sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) {
    echo "This sanitized email address is considered valid.";
} else {
    echo "This (b) sanitized email address is considered invalid.\n";
}

$sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL);
if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) {
    echo "This (c) sanitized email address is considered valid.\n";
    echo "Before: $c\n";
    echo "After:  $sanitized_c\n";    
}

I've found this useful for socket server communication, especially when passing JSON around, as a null character causes json_decode() to return null.

我发现这对于套接字服务器通信很有用,尤其是在传递 JSON 时,因为空字符会导致 json_decode() 返回空值。

回答by SteAp

While it's probably not the primary target of your question, please have a look at PHP's filter functions: http://www.php.net/manual/en/intro.filter.php

虽然它可能不是您问题的主要目标,但请查看 PHP 的过滤器功能:http: //www.php.net/manual/en/intro.filter.php

Filter functions validate and sanitize values. Form the PHP site:

过滤器功能验证和清理值。形成 PHP 站点:

##代码##

Result:

结果:

This (a) sanitized email address is considered valid.

This (b) sanitized email address is considered invalid.

This (C) sanitized email address is considered valid.

Before: ([email protected])

After: [email protected]

此 (a) 已清理的电子邮件地址被认为是有效的。

此 (b) 已清理的电子邮件地址被视为无效

此 (C) 消毒电子邮件地址被认为是有效的。

之前:([email protected]

之后:[email protected]