Remove string length value from PHP array result - php

I have a PHP associative array that I am trying to extract data from:
array(18) { ["body"]=> string(34) "Hey! Let me know if you got this"}
The above array is stored in a variable called $firstChildData, and when I try to run the following line, I get the result below it:
$firstChildBody = $firstChildData["body"];
This returns: string(34) "Hey! Let me know if you got this"
Does anyone know how to remove the 'string(34)' so I can just have the value within the quotes? I have tried to use the explode() function with " as the delimiter, but that didn't work.
Thanks in advance!

string(34) is just a debug information if you use a function like var_dump or print_r.
You can print strings normal with echo or print.
echo $firstChildData["body"];
If you want to use the variable $firstChildData["body"] in any program code, it is handled as the string in it. The information string(34) ist not included.

Thanks everyone!
You're right, I was using var_dump, which was showing that information even thought it was not part of the string. I used echo instead and it works beautifully.

Related

Trim result with RegEx

Using the following in PHP:
var_dump($obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image)
Results:
string(56) "https://api.site.com/image_link/user-76643221_b691647a"
I only need the link, without those extra parts. Any ideas?
var_dump shows the types and sizes of variables; it's meant for debugging. If you only need the link, then instead of using var_dump use print or echo
echo $obj->denormalized->{'https://api.site.com/user/user-76643221'}->data->avatar_image;
Results:
https://api.site.com/image_link/user-76643221_b691647a

PHP base64_decode returning nothing

The site I am working on is using a .net Soap web service for getting data. The initial call returns XML that contains a base64 encoded string. I am able to isolate that string by using $lastResponse = $client->__getLastResponse(); When I use var_dump on the $last response variable I get some thing like
string(10139) "77u/PD94bWwgdmV...=="
When I use echo I get 77u/PD94bWwgdmV...== without the string(10139) to start. I have tried to place the $lastResponse variable into the base64_decode function but it returns nothing at all, not even NULL. I have also tried to split the string first to remove the 77u/ from the start and that does not work at all.
$lastResponse = $client->__getLastResponse();
$splitResponse = preg_split("#/#", $lastResponse);
echo base64_decode($lastResponse);
echo base64_decode($splitResponse[1]);
var_dump($splitResponse[1]);
var_dump(base64_decode($splitResponse[1]));
echo $lastResponse;
The code above returns this to the browser:
string(0) "" string(0) "" 77u/PD94bWwgdmVyc2lvbj0....
But when I copy/paste everything after the 77u/ into an only decoder I get the decoded xml that I am supposed to have returned to me. I am very confused as to what I am missing here any help will be greatly appreciated.

Get attribute value from JSON

object(stdClass)#43 (3) { ["#promo"]=> string(4) "true" ["#rate"]=> string(6) "308.69" ["#baseRate"]=> string(6) "342.99" }
The above is the partial output from a JSON file. The original file is actually an XML file.
<NightlyRate promo="true" rate="227.09" baseRate="283.86"/>
I used jsondecode to convert the XML to json. However, I don't know how to get the values for each of the attribute.
Ex, i want to get 227.09 and 283.86 using JSON.
Thanks so much for your time.
I do find the entire process a bit odd, but you can access invalid variable name properties of stdClass using this syntax:
$stdClassVar->{"#rate"};

var_dump outputting empty string - php

Hi I am performing an SQL query (that only returns one row) and I am printing a particular column on screen just for test purposes. The problem I am having is, the column that I am printing contains a string of 12 characters (abcdef123456), yet sometimes the printed variable is empty.
So when I do var_dump($test);
sometimes I get:
string(12) "abcdef123456"
but some other times I get
string(12) ""
I dont understand why it knows there are 12 characters yet its still empty, and sometimes it says 12 characters yet its full. Due to this, I cant perform other functions as they rely on the string.
EDIT: here is the query
$query="SELECT * FROM members WHERE name='$member'";
$sqlResult=mysql_query($query);
$row = mysql_fetch_assoc($sqlResult);
$test = $row["membercode"];
var_dump($test);
You most-likely have html tags in your string that are being rendered by the browser.
$foo = "<p><img><div><table>";
var_dump($foo); // string(20) ""
Where do you see that it says string(12) ""? Remember to ALWAYS look in the raw output; the source code. Not how the browser renders it.
For instance, if the string is <span></span>, that'll show up as nothing.
Try adding header('Content-Type: text/plain') to render your page as text instead of HTML. Or use "View Source" to view the HTML source instead of the rendered page. There may be hidden HTML tags in the string.
Try var_dump(bin2hex($test)). There may be invisible control characters such as "\0" that you can't see. For example:
$ php <<< '<?php var_dump("\0\0\0\0"); ?>'
string(4) ""
$ php <<< '<?php var_dump(bin2hex("\0\0\0\0")); ?>'
string(8) "00000000"

Can't unserialize a string in php

Yes, i know. I see a lot of questions about it.
But no one works for me until now.
I have a blog in wordpress who use serialized data for store some custom fields.
It works great, but when i moved all the blog to another folder, all serialized data gonne from wordpress (but it stills in the database)
So, wordpress don't detect it.
Now... i'm figthing with the code to know why isn't working.
At the end... i just thinked, well, i gonna do a code for get the serialized data and it will work.
Now i'm lost, i have this:
$data = 'a:7:{s:4:"zoom";s:2:"18";s:8:"latitude";s:8:"41.37233";s:9:"longitude";s:7:"1.04283";s:11:"address_one";s:16:"Finca Riudabella";s:11:"address_two";s:33:" s/n - 43430 Vimbodí (Tarragona)";s:3:"pin";s:77:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png";s:6:"bg_img";s:0:"";}';
$data = "a:7:{s:4:1}";
$data = trim($data);
var_dump($data);
var_dump(unserialize($data));
I tried with an original serialized string from the database (the fisrt line) and returns false and a error.
I done one a little bit simple, and say the same.
My error is:
Notice: unserialize() [function.unserialize]: Error at offset 5 of 11 bytes in C:\xampp\htdocs\unser.php on line 6
bool(false)
So, i don't know why i can't get data from the string!
I tried this tool http://unserialize.net/serialize and my data work just as expected :\ i need to do something else?
$broken_data = 'a:7:{s:4:"zoom";s:2:"18";s:8:"latitude";s:8:"41.37233";s:9:"longitude";s:7:"1.04283";s:11:"address_one";s:16:"Finca Riudabella";s:11:"address_two";s:33:" s/n - 43430 Vimbodí (Tarragona)";s:3:"pin";s:77:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png";s:6:"bg_img";s:0:"";}';
$data = serialize(
array(
"zoom" => "18",
"latitude" => "41.37233",
"longitude" => "1.04283",
"address_one"=>"Finca Riudabella",
"address_two"=>" s/n - 43430 Vimbodí (Tarragona)",
"pin"=>"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png",
"bg_img"=> ""
)
);
// The right data
var_dump($data);
// Your data.
var_dump($broken_data);
var_dump(unserialize($data));
As you can see the serialization of $data is correct.
The serialized $broken_data seems to have incorrect string length at "pin".
In $broken_data it's stated to be 77 characters (s:77) but in reality it's 67 characters long (s:67)
I ran into this exact same problem recently and spent countless hours finding a way to restore the bad data. I walked through every function and line of code in the Spyropress theme, until it led me to page after page of functions inside WordPress, and finally leading me to understand that the maybe_unserialize() was failing.
That led me on a quest to figure out why, where I stumbled across a few threads like this one, that pointed out how the string counts could be off. It turns out that a find/replace had been performed on the data throughout the entire database, ruining tons of pages built with a theme.
In my case, I had to automate a "fix", and I posted my code in another thread. I'm not sure if better to post here or just link there, so I'm linking. You can see my code to "fix" serialized data here:
Handy code to automatically fix broken serialized data string counts.
My solution works with large datasets, containing HTML/CSS, escaped double quotes, newlines, and tons of special characters. I thought it might help those who find this page before the other one (like I did).
Cheers!
You're assigning data twice.
First assignment
$data = 'a:7:{s:4:"zoom";s:2:"18";s:8:"latitude";s:8:"41.37233";s:9:"longitude";s:7:"1.04283";s:11:"address_one";s:16:"Finca Riudabella";s:11:"address_two";s:33:" s/n - 43430 Vimbodí (Tarragona)";s:3:"pin";s:67:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png";s:6:"bg_img";s:0:"";}';
It fails on this: s:77:"http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png"; but as you've may noticed length of provided url is 67, when you manually change it to 67, and use: var_dump( unserialize( $data)); You'll get this:
array(7) {
["zoom"]=>
string(2) "18"
["latitude"]=>
string(8) "41.37233"
["longitude"]=>
string(7) "1.04283"
["address_one"]=>
string(16) "Finca Riudabella"
["address_two"]=>
string(33) " s/n - 43430 Vimbodí (Tarragona)"
["pin"]=>
string(67) "http://espaciosparaeventos.es/wp-content/uploads/2012/02/fincas.png"
["bg_img"]=>
string(0) ""
}
How it got changed? I don't know, there doesn't seem to be special characters, maybe class implementing Serializable provides wrong string length for URL.
Second assignment
$data = "a:7:{s:4:1}";
This is just wrong, declaring array with 7 items, providing just one... And that one is string that is supposed to have length 4 ("str1") and instead providing just 1... This just shouldn't and cannot work.

Categories