PHP xmlreading 和 strlen/if 语句错误
我得到一个 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<pluginlist>
<plugin>
<pid>1</pid>
<pluginname>ChatLogger</pluginname>
<includepath>pugings/</includepath>
<cmds>say
</cmds>
<cmds>sayteam</cmds>
<cmds>tell</cmds>
</plugin>
</pluginlist>
一个 php 是这样的:
<?php
$xml_pluginfile="pluginlist.xml";
if(!$xml=simplexml_load_file($xml_pluginfile)){
trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach($xml as $plugin){
echo $plugin->pid." : ";
foreach($plugin->cmds as $value)
{
echo $value." ". strlen(value)."<br />";
}
echo "<br />";
}
?>
我得到的输出是:
1 : say 5
sayteam 5
tell 5
为什么我得到每个输出的长度为 5?
当我尝试这样做时:
if($value)=="say"
为什么会发生这种情况?
请帮我 谢谢
I got an xml file :
<?xml version="1.0" encoding="utf-8"?>
<pluginlist>
<plugin>
<pid>1</pid>
<pluginname>ChatLogger</pluginname>
<includepath>pugings/</includepath>
<cmds>say
</cmds>
<cmds>sayteam</cmds>
<cmds>tell</cmds>
</plugin>
</pluginlist>
And a php was something like this :
<?php
$xml_pluginfile="pluginlist.xml";
if(!$xml=simplexml_load_file($xml_pluginfile)){
trigger_error('Error reading XML file',E_USER_ERROR);
}
foreach($xml as $plugin){
echo $plugin->pid." : ";
foreach($plugin->cmds as $value)
{
echo $value." ". strlen(value)."<br />";
}
echo "<br />";
}
?>
The output i get is :
1 : say 5
sayteam 5
tell 5
Why do i get the length of each output as 5 ?
and when i try to do this :
if($value)=="say"
Why is this happening ?
Please help me
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您上面粘贴的 XML 文件在“say”之后和“
”之前有一个“\n\t”,因此它们是 2 个额外字符。
\n 换行
\t 对于选项卡,
您可以使用“trim()”来清理它们。
编辑::
完全错过了
你的语句
应该是 $value 而不是 strlen() 中的 value;
:)
the XML file you pasted above has a "\n\t" after the "say" and before the "
</cmds>
", so they are the 2 extra characters.\n for new line
\t for tab
you can use "trim()" to clean them.
EDIT::
TOTALL MISSED THAT
your statement
it should be $value instead of value in the strlen();
:)
这是因为标签之一中有空格。您可以通过删除空格或在 PHP 代码中使用以下内容来解决此问题:
另外:
strlen(value)
也应更改为strlen($value)
。It's because there it whitespace in one of the
<cmds>
tags. You can fix this by removing the whitespace or by using the following to your PHP code:Also:
strlen(value)
should also be changed tostrlen($value)
.错误在于 strlen() 使用文字字符串作为输入而不是变量;您错过了在值前面添加 $ 。
将您的 echo 替换为:
希望有帮助。
The error is that strlen() has a literal string as input rather than the variable; you missed to prepend value with a $.
Replace your echo with this:
Hope it helps.