Search code examples
phpjsonparsing

How to extract images from a string in PHP?


How do I get all the image links from this string? (into an array)

{                      "imagedata": {       "duration": 4000,       "images":{                "image1":{         "url":"/images/nature/flowers/pictureKUO133529A.jpeg",         "title":"",         "index":"01"         },         "image2":{         "url":"/images/nature/flowers/pic092533.jpg",         "title":"",         "index":"02"         },         "image3":{         "url":"/images/nature/flowers/pic092529.jpg",         "title":"",         "index":"03"         },         "image4":{         "url":"/images/nature/flowers/pic092531.jpg",         "title":"",         "index":"04"         },         "image5":{         "url":"/images/nature/flowers/pic092530.jpg",         "title":"",         "index":"05"         },         "image6":{         "url":"/images/nature/flowers/pic092534.jpg",         "title":"",         "index":"06"         },         "image7":{         "url":"/images/nature/flowers/0112678pic092532.jpg",         "title":"",         "index":"07"         },         "image8":{         "url":"/images/nature/flowers/0112881pic092656.jpg",         "title":"",         "index":"08"         },         "image9":{         "url":"/images/nature/flowers/0112880pic092655.jpg",         "title":"",         "index":"09"         }       }       }                     }

I have tried with preg_match_all() but i can't get it working.... I just want strings like /images/nature/flowers/0112880pic092655.jpg


Solution

  • Well, you can use a regex for that. But as it seems to be JSON, a simple call to json_decode() might do.

    Set the second parameter to TRUE if you want an array structure.
    Then access it like:

    print $array["imagedata"]["images"]["image1"]["url"];