I need to grab a json-string from this page: https://retracted.com
If you view the source, I json-string starts after var mycarousel_itemList =. I need to parse this string as a correct json-array in my php-script.
How can this be done?
EDIT: I’ve managed to pull this off using explode, but the method is ugly as heck. Is there no build-in function to translate this json-string to a array?
To clarify: I want the string I grab (which is correct json) to be converted into a php-array.
The JSON in the script block is invalid and needs to be massaged a bit before it can be used in PHP’s native
json_decodefunction. Assuming you have already extracted the JSON string from the markup (make sure you exclude the semicolon at the end):This will then give an array of arrays of the JSON data (demo).
In other words, the properties have to be double quoted and the values need to be in double quotes as well. If you want an array of
stdClassobjects instead for the “{}” parts, remove thetrue.You can do this either with
str_replaceas shown above or with a regular expression:The above code will fetch the URL, extract the JSON, fix it and convert to PHP (demo).