syntax error, unexpected '{' php [closed] - php

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
My php code gives the following error:
syntax error, unexpected '{' on line 8
PHP Code:
$data = '<?php
# 1 = ON; 0 = OFF.
$str = '{ //line 8
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
},
"Apps": [ {
"App1": 1,
"App2": 0,
"App3": 1,
"App4": 0,
"App5": 0,
"App6": 0
} ]
}';
echo $str;
?>';
I'm a newbie to php. Can anybody help me finding where I'm wrong? Thanks.

That's because the second ' in here:
+-- open string constant
V
$data = '<?php
# 1 = ON; 0 = OFF.
$str = '{
^
+-- close string constant
is terminating the string constant. You probably want to escape it (and the other one just before the final echo) such as:
$data = '<?php
# 1 = ON; 0 = OFF.
$str = \'{
blah, blah, blah
}\';
echo $str;
?>';

Your string got terminated by the single quote (apostrophe) right before the { character. Escape the ' character using \'.
$data = '<?php
# 1 = ON; 0 = OFF.
$str = \'{ //line 8
"name": "10.000000,106.000000",
"Status": {
"code": 200,
"request": "geocode"
},
"Apps": [ {
"App1": 1,
"App2": 0,
"App3": 1,
"App4": 0,
"App5": 0,
"App6": 0
} ]
}\';
echo $str;
?>';

Related

PHP Regular Expressions: How to write rules [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
How do I validate a string against the following rules:
$string = 'int(11)';
Rule: first 4 characters MUST be 'int('
Rule: next must be a number between 1 and 11
Rule: next must be a ')'
Rule: Everything else will fail
Experienced PHP Developer here - Regular Expressions are not my strong point..
Any help or suggestions welcome.
Thanks guys..
if (preg_match('/int\((\d{1,2})\)/', $str, $matches)
&& (int) $matches[1] <= 11 && (int) $matches[1] > 0
) {
// ... do something nice
} else {
echo 'Failed!!!'
}
Or if you want to not use the pReg library (can be faster):
$str = 'int(11)';
$i = substr($str, 4, strpos($str, ')') - 4);
if (substr($str, 0, 4) === 'int('
&& $i <= 11
&& $i > 0
) {
echo 'succes';
} else {
echo 'fail';
}
use this regular expression int\((\d|1[01])\)
int\(( first rule
(\d|1[01]) second rule
\) third rule
This regular expression is even smaller:
int\((\d1?)\)
or without the capturing group (if you don't need to retrieve the numeric value).
int\(\d1?\)

Reading JSON from remote URL in PHP [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
I have this JSON file:
http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json
and I am trying to decode it in PHP as follows:
$json = file_get_contents('http://www.jeewanaryal.com/angryQuiz/eighties/json/eighties.json');
$data = json_decode($json);
var_dump($data);
But, the output I am getting is NULL. Am I missing anything?
Using example from PHP.NET json_last_error() I found that your json syntax is not correct:
switch (json_last_error()) {
case JSON_ERROR_NONE:
echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
Output:
- Syntax error, malformed JSON
However, I checked your json code in the following website but it says valid:
http://www.freeformatter.com/json-validator.html
http://jsonlint.com/
http://json.parser.online.fr/
http://jsonformatter.curiousconcept.com/
Documentation
json_decode can return NULL as the documentation states.
Returns the value encoded in json in appropriate PHP type. Values
true, false and null (case-insensitive) are returned as TRUE, FALSE
and NULL respectively. NULL is returned if the json cannot be decoded
or if the encoded data is deeper than the recursion limit.
Your Problem
json_decode is failing here because of \n inside of id 6
{ "id": 6, "url":
"http://jeewanaryal.com/angryQuiz/eighties/images/betterOffDead.jpg",
"question": "In the movie 'Better Off Dead', what was the \n name of
Lane's younger brother?", "answer": [ { "a": { "text": "Bradger",
"status": 1 } }, { "b": { "text": "Peter", "status": 0 } }, { "c": {
"text": "Frank", "status": 0 } }, { "d": { "text": "Michael",
"status": 0 } } ] }
Solution
I guess your best bet here is to escape them before json_decode
$safe_json = str_replace("\n", "\\n", $json);
Please set the second parameter to true. This will give you an associative array. If the result is still null I'd be interested whether the file_get_contents returned something.
This is just a guess, but as you do not show what $json contains, I am going to assume that file_get_contents returned false, leading to the result you get.
You should check your php.ini file, what is the value for allow_url_fopen? If that is 0 that means that you cannot read a file from an http address so that could be the problem.
Just change the value of allow_url_fopen to 1 if that is the problem.

How to add +1 to a number through PHP? [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 10 years ago.
I am Having the data like:
$aa ="msg_1";
I want to add +1 at the end of string after doing the explode operation like the following:
$nwMsg =explode("_",$aa);
$inMsg =number_format($nwMsg[1])+1;
$finStr =$nwMsg[0].'_'.$inMsg;
After This i want to form the string again and repeating the same process again but it is increasing up to "10" after that it is not increasing...
You should put the +1 inside the number_format call, not after it.
EDIT: If you just want $nwMsg[1] to be treated as a number, just adding 1 to it will work fine, since + is a numerical operator.
function add_one($string) {
preg_match_all("/[a-zA-Z]+_\d+/", $string, $matches);
$elements = $matches[0];
$last = $elements[count($elements)-1];
$components = explode("_", $last);
$newnum = $components[1] + 1;
return $string . $components[0] . "_" . $newnum;
}
echo add_one("msg_1"); // prints "msg_1msg_2"
echo add_one("msg_1msg_2msg_3msg_4msg_5msg_6msg_7msg_8msg_9"); // prints "msg_1msg_2msg_3msg_4msg_5msg_6msg_7msg_8msg_9msg_10"
$nwMsg =explode("_",$aa);
$inMsg =number_format($nwMsg[1] +1) ;
$finStr =$nwMsg[0].'_'.$inMsg;
$aa= "msg_1";
$new_string= explode("_", $aa);
$new_aa= $new_string[0] ."10";
This is wrong
$inMsg =number_format($nwMsg[1])+1;
This is how it is done
$inMsg =number_format($nwMsg[1]+1);
$nwMsg =explode("_",$aa);
$inMsg =$nwMsg[1] +1 ;
$finStr =$nwMsg[0].'_'.$inMsg;
You will get the result with out using number_format.
One more thing, which can lead to errors and you need to take care - because you want to add two numbers, first make sure that you convert $nwMsg[1] into number (integer or float, it depends):
$nwMsg =explode("_",$aa);
$inMsg =number_format((int)$nwMsg[1]+1);
$finStr =$nwMsg[0].'_'.$inMsg;
How about a different solution:
function add($matches) {
return ++$matches[0];
}
$new = preg_replace_callback("(\d+)", "add", $aa);

PHP algorithm for CDN distribution [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
IGNORE THE QUESTION:
The CSS File I was including pulled in the the other files hence the correlation *facepalm*
We have the following code for picking a CNAME CDN reference per filename. It must return the same URL everytime based on a given filename. We thought this would be sufficiently random:
<?php
function cdn_prefix($fileName) {
$number_of_servers = 4;
$md5 = md5($fileName);
$md5 = substr($md5, 0, 4);
$hash_number = base_convert($md5, 16, 10);
$server_number = ($hash_number % $number_of_servers) + 1;
$server_prefix = '//static' . $server_number . '.' . $_SERVER['SERVER_NAME'];
return $server_prefix . $fileName;
}
?>
However it seems to favour the number 3:
No matter what I do (salt, different bases, random multiplication, etc) the results headerBg through to mainNavPipe (on the screen shot) all have the same number.
Is there a better algorithm?
EDIT:
Here are the results using same algorithm using a SHA1
Everywhere calls the same function - as it returns the whole URL and wouldn't show the static[1-4] domain unless it when through this function.
The array (for testing) is:
FILES = [
'/a/files/image/250.jpg',
'/a/files/image/244.jpg',
'/a/files/image/247.jpg',
'/a/css/global/core.css',
'/a/css/global/print.css',
'/a/img/global/new_logo.gif',
'/a/img/global/book-a-free-survey.gif',
'/a/img/global/make_an_enquiry.gif',
'/a/img/global/purchase-locks-blue.jpg',
'/a/files/image/251.jpg',
'/a/img/global/bg.gif',
'/a/img/global/headerBg.jpg',
'/a/img/global/basketBg.gif',
'/a/img/global/arrow.png',
'/a/img/global/trolley.gif',
'/a/img/global/mainNavBg.gif',
'/a/img/global/mainNavCurrentBg.gif',
'/a/img/global/mainNavPipe.gif',
'/a/img/common/sectionNavBg.jpg',
'/a/img/global/nav_arrow.gif',
'/a/img/global/footerBg.jpg',
'/a/img/global/footerCopyrightBg.jpg',
'/a/img/global/footerLogo.jpg'
]
This was probably a one-time thing or a bug elsewhere.
function cdn_prefix($fileName) {
$number_of_servers = 4;
$md5 = md5($fileName);
$md5 = substr($md5, 0, 4);
$hash_number = base_convert($md5, 16, 10);
$server_number = ($hash_number % $number_of_servers) + 1;
return $server_number;
}
$arr = array(1=>0, 2=>0, 3=>0, 4=>0,);
for ($i = 1; $i < 200000; $i++) {
$arr[cdn_prefix("anrg".$i)]++;
}
print_r($arr);
gives:
Array
(
[1] => 49770
[2] => 50090
[3] => 50026
[4] => 50113
)

Parse error help, PHP unexpected T_STRING (probably something simple) [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
It's probably something simple I am not seeing because I've stared at it too long.
Any ideas? It's throwing on line 119, I've indicated it below
Parse error: syntax error, unexpected T_STRING in /home6/cleanai4/public_html/act.php on line 119
I'm just trying to format a phone number.
if(isset($submit)):
$db = mysql_connect("localhost", "#######", "#######");
mysql_select_db("###########", $db);
$date = date("Y-m-d");
$address = $street . ", " . $city . " " . $zip;
Line 19-> $phonetmp = '('substr($phone, 0, 3)')' . substr($phone, 3, 3) . '-' . substr($phone, 6);
$phone = $tmp;
$sql = "INSERT INTO ########
VALUES(NULL,'$name', '$address', '$email', '$phone', '$info', '$sign', '$date' )";
mysql_query($sql);
print("<h2>We appreciate your support</h2>\n");
print("<b>Now, spread the word</b><hr>\n");
endif;
You're missing the concatenation after the first part of $phonetmp, should be '(' . substr($phone, 0 , 3) . ')'...
Also note: unless you have a variable called $tmp outside of the code segment, you're setting $phone to an undeclared variable. And make sure you sanitize user inputs!
That's not a password I'm seeing in the mysql_connect call is it? ;)

Categories