This is a strange one for me, i am parsing an rss feed using simplexml_load_file, it's working fine up until:
<?php
$xml = simplexml_load_file($url);
$items = $xml->channel->item;
foreach($items as $offer)
{
echo $offer->title;
echo "<br />";
echo $offer->guid;
echo "<br />";
echo $offer->description;
echo "<br />";
echo $offer->campinfo:amount;
echo "<br />";
echo $offer->campinfo:country;
echo "<br />";
echo $offer->campinfo:type;
echo "<br />";
echo "<hr>";
?>
It hits these parts:
$offer->campinfo:amount; the ":" is causing the script to error out, Parse error: syntax error, unexpected ':', expecting ',' or ';'
I cannot find any information on this, any help would be appreciated.
EDIT: example added
<item>
<title>win iPhone 6s!</title>
<link>http://qckclk.com/offer.php?id=341201&pub=240627&subid=</link>
<guid>http://qckclk.com/offer.php?id=341201&pub=240627</guid>
<description>il suffit d';entrer votre numéro pour gagner 6s iPhone!</description>
<campinfo:amount>10.24</campinfo:amount>
<campinfo:campid>341201</campinfo:campid>
<campinfo:country>LU</campinfo:country>
<campinfo:type>Pin+Submit</campinfo:type>
<campinfo:epc>1.01</campinfo:epc>
<campinfo:ratio>9</campinfo:ratio>
</item>
: is not valid in a variable name. If you need to access a property that isn't a valid identifier, you need to use {"string"} notation:
echo $offer->{"campinfo:amount"};
http://php.net/manual/en/language.variables.basics.php
A valid variable name starts with a letter or underscore, followed by
any number of letters, numbers, or underscores
As a regular expression, it would be expressed thus:
'[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]*'
echo $offer->campinfo:amount;
contains : in the variable, which isnt legal according to the manual.
Change that or var_dump ($offer) to see whats actually in there
For anyone needing to know in future the answer was:
$offer->children('campinfo', true)->amount;
Related
i'm getting a string from an mssql -database to my .php-page.
For good look, I want to replace the newlines with <br />
so I tried the following (one at a time):
echo nl2br($data);
echo str_replace(chr(10), "<br />",str_replace(chr(13), "<br />", $data))
echo str_replace("\n", "<br />",str_replace("\r", "<br />", $data))
The HTML-source code looks all right:
blablalba<br />sdsddsfdfs<br />fds<br />dfsdfs<br />fdsdsf<br />:_k,ölmjlö<br />öä.löälöä#<br />
But the result on the HTML is empty, and Chrome developer-tools is displaying following:
<br><br><br><br><br><br><br>
What am I missing?
echo $data is giving me the right result but without <br />'s
blablalba sdsddsfdfs fds dfsdfs fdsdsf :_k,ölmjlö öä.löälöä#
Regards
Use Wordwrap...
Wrap a string into new lines when it reaches a specific length:
<?php
$str = "An example of a long word is: Supercalifragulistic";
echo wordwrap($str,15,"<br>\n");
?>
Result:
An example of a
long word is:
Supercalifragulistic
I am getting error by running following code:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var=>$value.'<br />'; //this will result in to following error:
//Parse error: syntax error, unexpected '=>' (T_DOUBLE_ARROW), expecting ',' or ';' in
//C:\xampp\htdocs\w_j_gilmore\CH_03_PHP_BASICS\superglobal.php on line 6
}
?>
And following code runs successfully
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo "$var=>$value<br />";
}
?>
Printing in single quotation and double quotation is difference.
WHY?
The difference between the 2 is that in the first you are trying to use the => operator (which is not valid in that place, so will result in a syntax error), while in the second you print a string which happens to have the characters = and > in it.
The second effectively could be rewritten as:
<?php
//superglobal.php
foreach($_SERVER as $var=>$value)
{
echo $var . "=>" . $value . "<br />";
}
?>
If you are just trying to output $_SERVER for debug reasons, I suggest using var_dump or print_r
var_dump($_SERVER);
You have not quoted the string:
echo $var=>$value.'<br />';
quoted would look like this:
echo '$var => $value <br />';
if single quoted, variables are not interpreted.
I want to echo these two strings. This is my current code:
echo str_replace(array_keys($swears), array_values($swears), $main).<br />;
echo str_replace(array_keys($swears), array_values($swears), $post).<br />;
But it produces this error:
Parse error: syntax error, unexpected '<' in C:\xampp\htdocs\new\new.php
Please let me know your feedback.
Use this instead since you have to use quotes for displaying HTML tags:
echo str_replace(array_keys($swears), array_values($swears), $main)."<br />";
echo str_replace(array_keys($swears), array_values($swears), $post)."<br />";
echo str_replace(array_keys($swears), array_values($swears), $main) . '<br />';
Ths is a string, so it has to be quoted.
echo str_replace(array_keys($swears), array_values($swears), $main). '<br />';
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 following code keeps giving me this error
Parse error: syntax error, unexpected T_VARIABLE in ...
?
$query_string = 'this is a test... "this is in quotes" mmm..chicken burgers... yummm...';
preg_match_all("/\".*\"|[^\s]*/", $query_string, $matches);
echo "Matches:";
foreach($matches[0] as $token) {
echo $token . "<br />";
}
it is from this web page
Have you looked at the line referred to in the Error Message you noted?
Have you looked at the lines preceding that line, to ensure that you have ended each line with the semi-colon ";", that you have used the correct operators for joining variables ".", etc.?
This sounds like a simple PHP Syntax error.
I just ran the following code on my XAMPP server with no error messages apparent:
<?php
$query_string = 'this is a test... "this is in quotes" mmm..chicken burgers... yummm...';
preg_match_all("/\".*\"|[^\s]*/", $query_string, $matches);
echo "Matches:";
foreach($matches[0] as $token) {
echo $token . "<br />";
}
As Col. Shrapnel noted, you have hidden dash symbol (173 decimal, Hex 00ad) in your code right before $query_string. Remove that and you'll be much better.
Update: to be exact, you have [comma], [space], [space], [hidden dash], [space], '$query_string'.