简体   繁体   English

PHP xmlreading 和 strlen/if 语句错误

[英]PHP xmlreading and strlen/if statement error

I got an xml file:我有一个 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>

And a php was something like this: 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 />";
        }
?>

The output i get is:我得到的 output 是:

1 : say 5
sayteam 5
tell 5

Why do i get the length of each output as 5?为什么我得到每个 output 的长度为 5?

and when i try to do this:当我尝试这样做时:

if($value)=="say"

Why is this happening?为什么会这样?

Please help me thanks请帮帮我谢谢

<cmds>say
        </cmds>

the XML file you pasted above has a "\n\t" after the "say" and before the " </cmds> ", so they are the 2 extra characters.您在上面粘贴的 XML 文件在“say”之后和“ </cmds> ”之前有一个“\n\t”,因此它们是两个额外的字符。

\n for new line \t for tab \n 换行 \t 制表符

you can use "trim()" to clean them.你可以使用“trim()”来清理它们。

EDIT::编辑::

TOTALL MISSED THAT完全错过了

your statement你的陈述

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

it should be $value instead of value in the strlen();它应该是 $value 而不是 strlen() 中的 value;

:) :)

It's because there it whitespace in one of the <cmds> tags.这是因为它在<cmds>标记之一中有空格。 You can fix this by removing the whitespace or by using the following to your PHP code:您可以通过删除空格或在 PHP 代码中使用以下内容来解决此问题:

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

Also: strlen(value) should also be changed to strlen($value) .另外: strlen(value)也应该更改为strlen($value)

The error is that strlen() has a literal string as input rather than the variable;错误在于 strlen() 有一个文字字符串作为输入而不是变量; you missed to prepend value with a $.你错过了用 $ 前置值。

Replace your echo with this:用这个替换你的回声:

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

Hope it helps.希望能帮助到你。

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

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