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'.
Related
Consider this :
$a = 'u';
$b[$a] = 'v';
$c[$b[$a]] = 'w';
This works fine:
php > echo $c[$b[$a]];
w
This also works:
php > echo '$c[$b[$a]]';
'$c[$b[$a]]'
However this leads to a syntax error:
php > echo "$c[$b[$a]]";
PHP Parse error: syntax error, unexpected '[', expecting ']'
While a shorter version works:
php > echo "$b[$a]";
v
Why?
In a double quoted string there are two kinds of variable parsing, simple and complex. Simple parsing will only work with a single dimensional array or object. So these will work:
echo "Valid: $foo[1]";
echo "Valid: $foo[bar]";
echo "Valid: $foo->bar";
But these will not:
echo "Invalid: $foo[1][2]";
echo "Invalid: $foo->bar->baz";
echo "Invalid: $foo->bar()[2]";
Complex parsing is what halfer suggested in his answer, which wraps the expression in curly braces, and will indeed solve your problem:
echo "Valid: ${foo[1][2]}";
echo "Valid: ${foo->bar->baz}";
echo "Valid: ${foo->bar()[2]}";
The short answer is: don't write PHP like this! If it is confusing to read then use an intermediate variable.
However, it can be fixed: it looks like PHP just wants the brace delimiters at the outermost edges of the variable:
<?php
$a = 'u';
$b[$a] = 'v';
$c[$b[$a]] = 'w';
echo "{$c[$b[$a]]}";
This is also fine as well:
echo "${c[$b[$a]]}";
In the first case, the whole thing is wrapped in braces, and in the second, the $ is allowed outside of the brace construct to say that the next bit is a variable.
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 am running a RST to php conversion and am using preg_match.
this is the rst i am trying to identify:
An example of the **Horizon Mapping** dialog box is shown below. A
summary of the main features is given below.
.. figure:: horizon_mapping_dialog_horizons_tab.png
**Horizon Mapping** dialog box, *Horizons* tab
Some of the input values to the **Horizon Mapping** job can be changed
during a Workflow using the internal programming language, IPL. For
details, refer to the *IPL User Guide*.
and I am using this regex:
$match = preg_match("/.. figure:: (.*?)(\n{2}[ ]{3}.*\n)/s", $text, &$result);
however it is returning as false.
here is a link of the expression working on regex
http://regex101.com/r/oB3fW7.
Are you sure that the line break is \n, is doubt, use \R:
$match = preg_match("/.. figure:: (.*?)(\R{2}[ ]{3}.*\R)/s", $text, &$result);
\R stands for either \n, \r and \r\n
My instinct would be to do some troubleshooting around the s flag as well as the $result variable passed by reference. To achieve the same without any interference from dots and the return variable, can you please try this regex:
..[ ]figure::[ ]([^\r\n]*)(?:\n|\r\n){2}[ ]{3}[^\r\n]*\R
In code, please try exactly like this:
$regex = "~..[ ]figure::[ ]([^\r\n]*)(?:\n|\r\n){2}[ ]{3}[^\r\n]*\R~";
if(preg_match($regex,$text,$m)) echo "Success! </br>";
Finally:
If this does not working, you might have a weird Unicode line break that php is not catching. To debug, for each character of your string, iterate through all the string's characters
Iterate: foreach(str_split($text) as $c) {
Print the character: echo $c . " value = "
Print the value from this function: . _uniord($c) . "<br />"; }
Okay I have been having a little issue with the code below right now it pulls a bunch of data about a few stocks for me but I cant seem to get ride of the [ or ]. I thought it would be easy to remove the brackets by adding them to the end and the beginning of the preg_split but right when I do it just pops a error message.
Is there any command to remove all [ or all ] cause I know I have found a command in the past to remove all non numeric characters thank you so much btw
<?php
$Bloomberg = 'http://www.bloomberg.com/quote/AAPL:US';
$Bloomberghtml = file_get_contents($Bloomberg);
$BloombergStart = preg_split('/data_values":/',$Bloomberghtml);
$BloombergRawData = preg_split('/>/',$BloombergStart[1]);
$OpenTimeStart = preg_split('/exch_open_time":/',$BloombergRawData[0]);
$OpenTime = preg_split('/,"/',$OpenTimeStart[1]);
$CloseTimeStart = preg_split('/exch_close_time":/',$BloombergRawData[0]);
$CloseTime = preg_split('/,"/',$CloseTimeStart[1]);
$StartPriceData = preg_split('/,/',$BloombergRawData[0]);
echo $BloombergRawData[0];
echo "<br><br><br>";
echo $OpenTime[0];
echo "<br>";
echo $CloseTime[0];
echo "<br>";
print_r($StartPriceData);
?>
I'm not sure I fully understood your problem, but if you need to remove the '[' and ']' chars you can do that rather simply:
str_replace(array( '[', ']' ), '', $BloombergRawData);
Not the most elegant solution, but it should to the trick.
I'm trying to do a bbcode parser class that can create personalyzed tags, but I have some problem with urls
I've did all I need without particular problems thanks to regular expressions but I have a problem when I try to create a special tag who point to a specified URL.
In my class I've added a method like this:
<?
private function check_url_from_bbcode ($tag = "url", $css = null, $url_path = null) {
$regex_url = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
if (!isset ($css)) $css = $this->css_link;
$regex_url = $this->regex_url;
if (isset ($url_path)) $url_path = "$url_path/";
$this->bbcode = preg_replace ("/\[$tag\]([$regex_url]*)\[\/$tag\]/", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path$1\">$1</a>", $this->bbcode);
$this->bbcode = preg_replace ("(\[$tag\=([$regex_url]*)\](.+?)\[/$tag\])", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path$1\">$2</a>", $this->bbcode);
}
?>
the code works fine with classical urls [url]http://ciao.com[/url] & [url=http://ciao.com]ciao[/url]
but I have some problem with the case of a special url subject page as last.fm style, so [artist]Lemon Jelly[/artist].
The bblink is converted in < a href="http://ciao.com/artist/Lemon Jelly">Lemon Jelly< /a> (I've used the spaces < a> only to show the link code).
The link has the whitespaces on the href attribute so can't never work.
<?
private function check_url_from_bbcode ($tag = "url", $css = null, $url_path = null) {
$regex_url = " a-zA-Z0-9\:\/\-\?\&\.\=\_\~\#\'";
if (!isset ($css)) $css = $this->css_link;
$regex_url = $this->regex_url;
if (isset ($url_path)) $url_path = "$url_path/";
// begin of the problem
$this->bbcode = preg_replace ("/\[$tag\]([$regex_url]*)\[\/$tag\]/", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path".str_replace (" ", "+", $1)."\">$1</a>", $this->bbcode);
// end of the problem
$this->bbcode = preg_replace ("(\[$tag\=([$regex_url]*)\](.+?)\[/$tag\])", "<a title=\"Vai al link\" class=\"$css\" href=\"$url_path$1\">$2</a>", $this->bbcode);
}
?>
To avoid this, I've wrote a little part of code that change the href url portion with tha same url but with "+" in place of " " whitespace char, so [artist]Lemon Jelly[/artist] should became < a href="http://ciao.com/artist/Lemon+Jelly">Lemon Jelly< /a>
I'm not experienced with PHP and I'm not sure what is the problem, I've uses this syntax in other situation without encounter the problem.
can someone help me to find where I'm wrong?
the error type is PHP Parse error: syntax error, unexpected T_LNUMBER, expecting T_VARIABLE or '$' in /...
Please provide the 2-3 lines before and after the line that has the error (line number should be in the PHP parse error.
This doesn't sound like a regex problem... more like an escaping issue.
I'm rather new to SO, why couldn't i just commented to the question to ask this (as what i wrote isn't really an answer)
The parse error is due to the fact that $1 is not a valid PHP variable name. They must start with a letter or an underscore. The $1 variable populated in the preg_replace parameters are just valid inside the parameters.
Try something more like this:
$this->bbcode = preg_replace("/\[$tag\]([$regex_url]*)\[\/$tag\]/e", "'<a title=\"Vai al link\" class=\"$css\" href=\"$url_path'. str_replace(' ', '+', '$1'). '\">$1</a>'", $this->bbcode);
The e modifier evaluates the replacement string as PHP code, so that should generate the string you want.
Note, I can't test it right now, so you may need to tweak it a bit. Sorry :]
Edit Never mind that, I got a hold of a FTP client. Fixed the regex. It works :)
To avoid this, I've wrote a little
part of code that change the href url
portion with tha same url but with "+"
in place of " "
Why not use urlencode on the contents of the tag?
Note that urlencode should only be used for query parameters; actual directory components should use rawurlencode, as HTTP itself doesn't use + instead of spaces.
I believe the problem is with the $1 reference, which is used in the str_replace function outside of the preg_replace arguments. The $1 backreference only works within the confines of preg_replace arguments, so you can't pass it like a variable to str_replace; it tries to use $1 as a variable, but in PHP that is invalid for a variable name.