I don't know how to work with regex.
I get from file_get_contents (So is a string):
"sources": [{"file":"https:\/\/www40.playercdn.net\/98\/0\/Ya2t561cTjn95vvqwekm7w\/1475599977\/161003\/839RHzKW43tP1DAt.mp4","label":"360p","default":"true"},{"file":"https:\/\/www61.playercdn.net\/100\/1\/sRVvFxxRF2pk26D8D7C_1A\/1475599977\/161003\/950LRfeY1xeAfNQ3.mp4","label":"720p"}] ,"logo": {
And I need to extract only
https:\/\/www40.playercdn.net\/98\/0\/Ya2t561cTjn95vvqwekm7w\/1475599977\/161003\/839RHzKW43tP1DAt.mp4
and
https:\/\/www61.playercdn.net\/100\/1\/sRVvFxxRF2pk26D8D7C_1A\/1475599977\/161003\/950LRfeY1xeAfNQ3.mp4
And need to know that first URL is 360p and second 720p
Thank you
I would not use regex for this. Instead use json_decode to convert the data to an array. Json is basically string-based objects that can be easily transmitted over the internet.
$array = json_decode($data, true);
You can now use normal php array notation to get the values you want.
Related
I have an array with multiple items in it one of which is an already encoded json string. I'm wanting to json encode the whole array but in doing so it re-json_encodes the json and adds slashes to it. The only way I've found to fix this is to json_decode the value and then encode the whole array. I feel like this is a waste of resources though and I feel like there has to be a better way. Is doing it that way the best possible way?
Here's a sample of the array I'm trying to json_encode.
$arr = array();
$arr["var1"] = '{"test":"test"}';
$arr["var2"] = 'foo';
$arr["var3"] = 'bar';
If I don't decode the var1 first and I just encode the whole array I get a result like so
{"var1":"{\"test\":\"test\"}","var2":"foo","var3":"bar"}
Notice the slashes in the json object.
json_encode() returns a string containing the json representation of a value.
In the example, a php string is passed as one element of the array '{"test":"test"}', thus json_encode() is encoding it appropriately into json format, with escaped quotes "{\"test\":\"test\"}".
If decoding nested json is a very resource heavy task, a workaround is to use a placeholder instead of the value, {"var1":"PLACEHOLDER","var2":"foo","var3":"bar"}, and then using str_replace() to replace it.
However, simply decoding it as you described is probably a cleaner solution, if its not resource heavy.
I am struggling with converting a string into an array:
["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]
and I want to convert it into an array of values inside quotes "".
Tried (un)serialize(), parse_str(). They don't cope with it.
Since nobody else is going to post it, that looks like JSON:
$array = json_decode($string, true);
print_r($array);
The true parameter isn't needed for this JSON, but if you want to make sure you always get an array and not an object regardless of the JSON then use it.
It would be easiest to use json_decode:
json_decode('["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]', true)
But if for some reason you are not parsing is as json, you should be able to use explode:
explode(',', '"Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"');
If you need to deal with the brackets, you can trim them from the string with something like this prior to exploding:
$string = trim('["Пятницкое шоссе","Митино","Волоколамская","Планерная","Сходненская"]', '[]');
I have the following json encoded object:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
and I want to convert this into url string so I can use it with my REST API function for example like this one:
search?search=asdadd%2C+United+Kingdom&type=tutor
I have found many functions in javascript to do this but I haven't been able to find anything in PHP. What is the function in PHP to do this?
The following query string:
?username=my_username&email=my_email&password=12345678&confirm_password=12345678
.. will turn into:
{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}
If you use json_enconde.
To reverse the process, you need to use json_decode as well as http_build_query.
First, turn the JSON into an associative array with json_decode:
$json = '{"username":"my_username","email":"my_email","password":"12345678","confirm_password":"12345678"}';
$associativeArray = json_decode($json, true);
Now use http_build_query with the new associative array we've built:
$queryString = http_build_query($associativeArray);
Result: username=my_username&email=my_email&password=12345678&confirm_password=12345678.
I have a string, more specifically, this one:
a:16:{s:9:"pseudonym";O:16:"SimpleXMLElement":0:{}s:14:"parallel_title";O:16:"SimpleXMLElement":0:{}s:9:"title_var";O:16:"SimpleXMLElement":0:{}s:6:"series";O:16:"SimpleXMLElement":0:{}s:9:"vol_title";O:16:"SimpleXMLElement":0:{}s:9:"reference";O:16:"SimpleXMLElement":0:{}s:10:"bound_with";O:16:"SimpleXMLElement":0:{}s:15:"general_remarks";O:16:"SimpleXMLElement":0:{}s:6:"copies";O:16:"SimpleXMLElement":1:{i:0;s:1:"1";}s:11:"remarks_BPH";O:16:"SimpleXMLElement":0:{}s:3:"ICN";O:16:"SimpleXMLElement":1:{i:0;s:4:"neen";}s:10:"provenance";O:16:"SimpleXMLElement":0:{}s:7:"binding";O:16:"SimpleXMLElement":0:{}s:10:"size_hxwxd";O:16:"SimpleXMLElement":0:{}s:6:"BookID";O:16:"SimpleXMLElement":1:{i:0;s:4:"6271";}s:5:"repro";O:16:"SimpleXMLElement":0:{}}
Is it possible to parse this string somehow? I need to display the keys and values in a list. I tried to use json_decode but it doesn't return anything, even with the second parameter set to true:
json_decode($string,true);
It's not JSON, it's serialized PHP. Use unserialize().
It's serialize object
Read more on PHP website
Im running a javascript code which reads values from different XML files and it generates a multidimentional array based on those values. Now I need to pass this array to a PHP page. I tried different but it always pass the arrray as string not as an array.
Anyone has an idea :( ... and thank you very much
What Caleb said.
Use this and JSON encode your JS array to a string, send it over to PHP and use json_decode to decode it into a PHP array.
You need a JSON encoder/decoder to do that. Prototype has it implemented by default and with jQuery you can use jQuery-JSON
For example if you use Prototype as your JS library then you can convert your array into a string like that:
var example_multi_dim_arr = {"a":[1,2,3], "b": [4,5,6]};
var string_to_be_sent_to_server = Object.toJSON(example_multi_dim_arr);
And in the PHP side (assuming that the JSON string is passed to the script as a POST variable)
$multi_dim_arr = json_decode($_POST["variable_with_json"], true);
The last true field in json_decode indicates that the output should be in the form of an array ($multi_dim_arr["a"]) and not as an object ($multi_dim_arr->a).
NB! the function json_decode is not natively available in PHP 4, you should find a corresponding JSON class if you are using older versions of PHP. In PHP 5 everything should work fine.