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.
Related
<?php
$color='red';
$lines=file("new.txt");
foreach ($lines as $line) {
if($line=='<~~~>'){
if($color=='red')
echo $color='yellow';
elseif($color=='yellow')
echo $color='red';
}
else{
echo $line."<br>";
}
}
?>
File Content:
hey there how are you
<~~~>
I am fine What about you?
<~~~>
I am also good. thank you.
<~~~>
Output is coming:
hey there how are you
<~~~>
I am fine What about you?
<~~~>
I am also good. thank you.
yellow
There's a trailing newline char at the end of each line. Use trim($line) to get rid of it :
<?php
$color = 'red';
$lines = file("new.txt");
foreach ($lines as $line) {
if (trim($line) == '<~~~>') {
if ($color == 'red')
echo $color = 'yellow';
else if ($color == 'yellow')
echo $color = 'red';
echo "<br />"; // Added to keep the original format
} else {
echo $line . "<br />";
}
}
Output :
hey there how are you
yellow
I am fine What about you?
red
I am also good. thank you.
yellow
It is explained in the documentation of PHP file() function:
Return Values
Returns the file in an array. Each element of the array corresponds to a line in the file, with the newline still attached.
This means, except for the last line, in case it doesn't end with a new line character, no line in your file can be equal to string '<~~~>'.
The solution(s) are also provided in the same documentation page, one paragraph below:
Note:
Each line in the resulting array will include the line ending, unless FILE_IGNORE_NEW_LINES is used, so you still need to use rtrim() if you do not want the line ending present.
Your problem is
echo $color='yellow';
just do
echo 'yellow'
instead. Same goes for the echo 'red'
The expression "$color = 'yellow'" produces a return value of 'void' which then is seen by the "echo". So actually you're doing a "echo void;" equivalent.
I am a weird issue regarding my class property here
I have the following:
$this->tableData = '<table>';
$this->tableData .= $string;
echo $this->tableData => output <table>
I want to concatenate more string to my $this->tableData but it seems like nothing is added.
I know $string is not null and contains characters
Did I do something wrong here?
Thanks!
To see if your string is not null you should use var_dump() or print_r() functions.
Example:
$this->tableData = '<table>';
echo "Dumping tableData: " . var_dump($this->tableData);
$this->tableData .= $string;
echo "Dumping tableData 2: " . var_dump($this->tableData);
echo "Dumping string: " . var_dump($string);
That way you will see exactly what is going on.
Is your variable $string containing a HTML tag, something like <p></p> or else ?
This could be "hidden" if you print_r it inside a browser.
So I have the following PHP code for a registration form:
<?php
$entries = array(
0 => $_POST['signup_username'],
1 => $_POST['signup_email'],
2 => $_POST['signup_password']);
$entries_unique = array_unique($entries);
$entries_unique_values = array_values($entries_unique);
echo " <br />".$entries_unique_values. " ";
?>
... And I'm realizing my echo syntax is wrong. How could I echo the different values of my array, without assigning a variable to each of my keys (there are a number of reasons as to why I can't do that)? I'd rather not use the r_print function as well.
Thanks in advance!
How do you want to output them? Comma-separated? Each on its own line? You have plenty of options. This should do the trick for comma-separated:
echo " <br />".implode(', ', $entries_unique). " ";
That said, be careful just outputting user input directly in HTML. This will leave you wide open to XSS vulnerabilities and invalid HTML in general. To output user input in HTML safely, you need to properly HTML encode the output. This would be preferable to the line above:
echo " <br />".implode(', ', array_map('htmlspecialchars', $entries_unique)). " ";
See implode(), array_map(), and htmlspecialchars().
Take a look at php's var_export().
var_export — Outputs or returns a parsable string representation of a variable
Try the implode() function:
echo implode(', ', array_values($entries));
he foreach loop is really easy for arrays, especially single associate arrays.
foreach($entries_unique as $key => $value) {
echo "key: " . $key . " - value: " . $value . "<br/>";
}
Check out php.net: http://php.net/manual/en/control-structures.foreach.php
Why Is my line break not working?
for($n=1; $n<=100; $n++)
{
echo $n '\n';
}
?>
You have syntax error there, it should be $n . '\n'
You are using ' single quote to quote the new line (\n), thus it's being interpreted as literal \ and n, change your code to: $n . "\n" to make it outputs as newline
Final code:
for($n = 1; $n <= 100; $n++)
{
echo $n . "\n"; // or "$n\n" (thanks #ring0 for pointing that out)
}
new lines are ignored in HTML. Use <br /> instead:
for($n=1; $n<=100; $n++)
{
echo $n . '<br />';
}
Use the dot (.) operator for string concatenation.
echo $n . "\n";
It needs to be in doule quotes:
Echo $n . "\n";
First thing your concatination is wrong. It should be
echo $n ."\n";
Next thing, if you are using it to output in browser, you should use <br />
echo $n."<br />";
If you are writing it to files or console and you want to be platform independent, use PHP_EOL
echo $n.PHP_EOL
Well, also remember that Newline characters are totally ignored in HTML (otherwise you'd have to do markup all on one line!)
If you're looking to get that effect, I'd recommend wrapping your output in nl2br, which converts your newlines into HTML breaks "" so that they display properly.
nl2br($n . "\n");
or just
echo $n . "<br>";
I am trying to print a variable between curly braces as
Product_number{product_version}
I tried
echo "$product_number{$product_version}";
But that does not work. I don't understand why :(
try using double braces:
echo "$product_number{{$product_version}}";
You can also do:
echo "$product_number{".$product_version."}";
{ followed by $ is treated specially. It is mainly used when you want to append a string immediately at the end of a variable's value:
$v = 'hack';
echo "I {$v}ed it";
echo $product_number . "{" . $product_version . "}";
Escape the "{":
echo "$product_number\{$product_version}";