简体   繁体   中英

PHP xmlreading and strlen/if statement error

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

<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.

\n for new line \t for tab

you can use "trim()" to clean them.

EDIT::

TOTALL MISSED THAT

your statement

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

it should be $value instead of value in the strlen();

:)

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:

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) .

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:

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

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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