1

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

3 Answers 3

2
<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();

:)

Sign up to request clarification or add additional context in comments.

3 Comments

1)Then why do on sayteam still i get 5 ?
Edited answer, you have "value" in strlen, instead of $value, PHP is assuming it to be constant string "value" and hence strlen = 5
I know i noticed it when @TimCooper i noticed it I DONT KNOW HOW I COULD BE SUCH A BLIND lol
1

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

2 Comments

1)Then why do on sayteam still i get 5 ?
You can see output i just pasted it here
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.