my serialized data looks like this
a:6:{i:0;s:12:"Early humans";i:1;s:32:"Apes And Hominids Dart's Find";i:2;s:36:"The hunter gatherers of South Africa";i:3;s:25:"Early Humans The Eve Gene";i:4;s:50:"Australopithecines, Homo ergaster and Homo sapiens";i:5;s:21:"Landscapes of history";}
When i try and unserialize, it turns out to be empty.
my code:
$urlnames = unserialize($row['url_name']);
foreach($urlnames as $names){
echo $name;
}
What am I doing wrong?
Thanks
The serialized string is wrong. There should be one more character in the "Dart's find" string -- I don't know why one is missing, but adding one will make it work fine.
See for yourself.
On index 1 you have only 31 characters that's why it isn't working.
Did you get your serialized string form the db?
If you get it from there, than your backslash is missing. That's your 32th character.
more info about backslashes and databases check google/yahoo/bing ...
Related
I am getting a variable from a socket. After reading the respond I am trying to convert the value to a number. if I print the respond it looks correct however when I convert it to number using floor() it doesn't give me the right answer. I also tried to print the length of the variable and it is still not working as it suppose to: This one is for value :185
echo("**************** ".floor($res[0]));
echo "################### $res[0]";
echo "------------- ".strlen($res[0]);
output:
**************** 1################### 185------------- 12
I have also tried stripslashes, trim and also ereg_replace('[:cntrl:]', '',$res[0])
Please try the following: intval($res[0])
You can try also with:
$res = (int)$reg[0];
Hope this helps.
Bye
Ok I found the problem. I saved the value in a file and opened the file using notepad++. I saw the following character in between my string:
SOH,NULL, and bunch of other non character values
What I am assuming is PHP automatically ignore the ASCII values are not show able on the screen (less than 21Hex).
I'm very new to PHP but have a good understanding of C,
When I want to access some post data on an API i'm creating in PHP I use:
$_POST['date_set']
to fetch a value being passed for date - This all works perfectly, however I read I should be fetching it like this:
$date_set = trim(urldecode($_POST['date_set']));
This always returns a 00:00:00 value for the date after it's stored in my DB.
When I access directly using $_POST['date_set'] I get whatever value was posted, for example: 2013-08-28 10:31:03
Can someone tell me what I'm messing up?
You should try it like,
$date_set = $_POST['date_set'].explode(' ');//('2013-08-28 10:31:03').explode(' ')
echo $date_set[1];
or
echo date('H:i:s',strtotime($_POST['date_set'])));
//echo date('H:i:s',strtotime('2013-08-28 10:31:03'));
If you are very new in php the Read date()
You only run urldecode over data is URL encoded. PHP will have decoded it before populating $_POST, so you certainly shouldn't be using that. (You might have to if you are dealing with double-encoded data, but the right solution there should be to not double encode the data).
trim removes leading and trailing white-space. It is useful if you have a free form input in which rogue spaces might be typed. You will need to do further sanity checking afterwards.
urldecode — Decodes URL-encoded string
Description
string urldecode ( string $str )
Decodes any %## encoding in the given string. Plus symbols ('+') are decoded to a space character.
urldecode: is used only for GET requests. you should be fine using $_POST['date_set'] only.
http://php.net/manual/en/function.urldecode.php
You'd better do this way
if(isset($_POST['date_set'])){
$date_set = $_POST['date_set'];
}
then you can use $date_set how you want.
If you still get 00:00:00 for $date_set, the problem is coming from the code which provide you the $_POST value.
I'm writing PHP code that uses a database. To do so, I use an array as a hash-map.
Every time content is added or removed from my DB, I save it to file.
I'm forced by my DB structure to use this method and can't use mysql or any other standard DB (School project, so structure stays as is).
I built two functions:
function saveDB($db){
$json_db = json_encode($db);
file_put_contents("wordsDB.json", $json_db);
} // saveDB
function loadDB(){
$json_db = file_get_contents("wordsDB.json");
return json_decode($json_db, true);
} // loadDB
When echo-ing the string I get after the encoding or after loading from file, I get a valid json (Tested it on a json viewer) Whenever I try to decode the string using json_decode(), I get null (Tested it with var_dump()).
The json string itself is very long (~200,000 characters, and that's just for testing).
I tried the following:
Replacing single/double-quotes with double/single-quotes (Without any backslashes, with one backslash and three backslashes. And any combination I could think of with a different number of backslashes in the original and replaced string), both manually and using str_replace().
Adding quotes before and after the json string.
Changing the page's encoding.
Decoding without saving to file (Right after encoding).
Checked for slashes and backslashes. None to be found.
Tried addslashes().
Tried using various "Escape String" variants.
json_last_error() doesn't work. I get no error number (Get null, not 0).
It's not my server, so I'm not sure what PHP version is used, and I can't upgrade/downgrade/install anything.
I believe the size has something to do with it, because small strings seem to work fine.
Thanks Everybody :)
In your JSON file change null to "null" and it will solve the problem.
Check if your file is UTF8 encoded. json_decode works with UTF8 encoded data only.
EDIT:
After I saw uploaded JSON data, I did some digging and found that there are 'null' key. Search for:
"exceeding":{"S01E01.html":{"2217":1}},null:{"S01E01.html":
Change that null to be valid property name and json_decode will do the job.
I had a similar problem last week. my json was valid according to jsonlint.com.
My json string contained a # and a & and those two made json_decode fail and return null.
by using var_dump(json_decode($myvar)) which stops right where it fails I managed to figure out where the problem was coming from.
I suggest var_dumping and using find dunction to look for these king of characters.
Just on the off chance.. and more for anyone hitting this thread rather than the OP's issue...I missed the following, someone had htmlentities($json) way above me in the call stack. Just ensure you haven't been bitten by the same and check the html source.
Kickself #124
In my particular case, I'm getting this error after doing an ExpressionEngine update from 1.x to 2.x. There is a table that manages site preferences, and the data for each field is a serialized PHP array. After the update, any serialized array instead just says czowOiIiOw==. Googling this string brings up other non-ExpressionEngine-related site forums where end users are reporting seeing this string while being unable to login.
Does anyone have any idea what might be causing this, or have you seen anything like this before?
It's an encoding of a serialization of an empty string.
>>> 'czowOiIiOw=='.decode('base64')
's:0:"";'
Somehow your preferences got wiped.
that is base64 encoded. it's :
s:0:"";
It's a base64-encoded version of s:0:"", which is a serialized PHP empty string.
Decoding it as base64 it becomes:
s:0:"";
As you can see if you execute the following line, it is a serialized empty string:
echo serialize(""); // Outputs s:0:"";
echo base64_encode(serialize("")); // Outputs czowOiIiOw==
For further information on the serialization spec, see this generous commenter's work, specifically Strings:
Anatomy of a serialize()'ed value:
...
String
s:size:value;
I'm having this problem that's driving me nuts.
I've got a MySQL database in which there is a table that contains a text field. I am querying the table in PHP and trying to put the content of the text field for each row into a var.
I am doing something like this:
for ($i=0;$i<$nbrows;$i++){
$id = $data[$i]['ID'];
$description = $data[$i]['DESCRIPTION'];
$mystring .= '<div>'.$id.': '.$description.'</div>';
}
DESCRIPTION is my text field.
I'll pass on the details. The $data array is built from mysql_fetch_array($result). I also tried using objects instead, as I use mysql_fetch_object for all my other routines, but there is no change.
Anyway, the problem is this: if I do "echo $description;" then it works. I am getting my text field data as expected. The problem is that I don't want to output it directly, but add it to a concatenated string, and that is not working. What happens in that case is it seems to be taking $description for some kind of array or object. To make the above example work, I have the replace the string with:
$mystring .= '<div>'.$id.': '.$description[0].'</div>';
So in the concatenated string code, if I treat $description as an array, it works, but obviously I am getting only one letter. (it doesn't actually seem to be an array because I can't implode it).
I tried a million things but I just can't make this work unless I use echo, but that is not what I am trying to do.
There is no issue with fields that aren't text.
Thanks for any ideas!
There is nothing visually wrong with the code you pasted, maybe if you could also add the fetching function as well, we might be able to help you further.
Maybe you could post a var_dump of your $data array?
Have you tried $mystring .= "<div> $id : $description </div>";
Ack, well, you know, hours spent on this and then it becomes obvious after I decide to post for help. This is just because of text encoding/escaping and nothing else. I just didn't see well enough where the problem was actually happening.
Thanks for taking the time to read and respond!