【发布时间】:2018-11-28 23:22:00
【问题描述】:
好的,伙计们,要么是我没有发现一些小问题,要么是该睡觉了。不管是什么,我都非常感谢您的建议。
我正在用 PHP 生成一个 XML 文件。一切都很顺利,直到我输入这一行:
$out .= "<name>" . trim($prod["name"], $prod["model"]) . "</name>";
因此发生了错误(以下日志)。
如果将其中一个变量更改为纯字符串,一切都会很好地工作。说,这两个都可以完美地工作:
$out .= "<name>" . trim($prod["name"], "123") . "</name>";
$out .= "<name>" . trim("123", $prod["model"]) . "</name>";
日志是这样写的:
mod_fcgid: stderr: PHP Warning: SimpleXMLElement::__construct(): Entity: line 1: parser error : Input is not proper UTF-8, indicate encoding !
mod_fcgid: stderr: Bytes: 0x9A 0xD0 0xB5 0xD0 in /xml/file.phpon line 193
mod_fcgid: stderr: PHP Warning: SimpleXMLElement::__construct(): picture><vendor>VES electric</vendor><stock_quantity>1000</stock_quantity><name> in /xml/file.php on line 193
mod_fcgid: stderr: PHP Warning: SimpleXMLElement::__construct(): ^ in /xml/file.php on line 193
mod_fcgid: stderr: PHP Fatal error: Uncaught Exception: String could not be parsed as XML in /xml/file.php:193
mod_fcgid: stderr: Stack trace:
mod_fcgid: stderr: #0 /xml/rozetka.php(193): SimpleXMLElement->__construct('<?xml version="...')
mod_fcgid: stderr: #1 {main}
mod_fcgid: stderr: thrown in /xml/file.php on line 193
再次感谢您的帮助。
【问题讨论】:
-
据我所知,
trim()-function 没有抛出任何错误?使用修剪后检查$out实际包含的内容。只是为了确保,您正在尝试从$prod['name']中删除$prod['model']中的字符?因为第二个参数是你实际上应该去掉的。 -
您确定您了解 trim 的第二个参数的作用吗?
-
@MagnusEriksson 第二个参数是字符掩码,因此模型中出现的所有符号都将从名称中删除
-
@IłyaBursov - 我知道。我在问 OP 是否有此意图。
-
@VladB。那么你不应该使用trim,而是str_replace,因为
trim('product pabc1', 'pabc1')会返回roduct
标签: php error-handling trim