繁体   English   中英

PHP xmlreading 和 strlen/if 语句错误

[英]PHP xmlreading and strlen/if statement error

我有一个 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 />";
        }
?>

我得到的 output 是:

1 : say 5
sayteam 5
tell 5

为什么我得到每个 output 的长度为 5?

当我尝试这样做时:

if($value)=="say"

为什么会这样?

请帮帮我谢谢

<cmds>say
        </cmds>

您在上面粘贴的 XML 文件在“say”之后和“ </cmds> ”之前有一个“\n\t”,因此它们是两个额外的字符。

\n 换行 \t 制表符

你可以使用“trim()”来清理它们。

编辑::

完全错过了

你的陈述

echo $value." ". strlen(value)."<br />";

它应该是 $value 而不是 strlen() 中的 value;

:)

这是因为它在<cmds>标记之一中有空格。 您可以通过删除空格或在 PHP 代码中使用以下内容来解决此问题:

foreach($plugin->cmds as $value)
{
    $value = trim($value); // removes whitespace from the start or end of
                           // the string
    // ...
}

另外: strlen(value)也应该更改为strlen($value)

错误在于 strlen() 有一个文字字符串作为输入而不是变量; 你错过了用 $ 前置值。

用这个替换你的回声:

echo $value . " " . strlen($value) . "<br />";

希望能帮助到你。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM