MidOhioIT Posted April 27, 2010 Share Posted April 27, 2010 Is there a way in php to read the following xml file and only parse out the string between the tags <image></image> as example and store in 3 variables. examples: $image1 = <image>http://www.buck.com/slideshow_images/sidesponsor1.jpg</image> (just inside the tags) $image2 = <image>http://www.buck.com/slideshow_images/sidesponsor2.jpg</image> (just inside the tags) $image3 = <image>http://www.buck.com/slideshow_images/sidesponsor3.jpg</image> (just inside the tags) <?xml version="1.0" encoding="utf-8" standalone="yes"?> <images> <pic> <image>http://www.buck.com/slideshow_images/sidesponsor1.jpg</image> <caption></caption> <link>http://www.wades.com</link> </pic> <pic> <image>http://www.buck.com/slideshow_images/sidesponsor2.jpg</image> <caption></caption> <link>http://www.facebook.com/pages/Bucom/164093073538?ref=ts</link> </pic> <pic> <image>http://www.buck.com/slideshow_images/sidesponsor3.jpg</image> <caption></caption> <link>http://www.carnohio.com/</link> </pic> </images> Quote Link to comment Share on other sites More sharing options...
Hybride Posted April 27, 2010 Share Posted April 27, 2010 You could use a regular expression (preg_match_all/replace, depending on what you want) to remove the <image></image> part in $image[$i]. For the actual XML, you could use SimpleXML. Example 2 looks like what you're trying to do. Quote Link to comment Share on other sites More sharing options...
MidOhioIT Posted April 27, 2010 Author Share Posted April 27, 2010 i dont want to remove the tags as much as I want to assign the string inside the first image tags to variable1 and inside the 2nd image tags to variable2 ect... does that make sense? Quote Link to comment Share on other sites More sharing options...
ldb358 Posted April 27, 2010 Share Posted April 27, 2010 as stated above use simpleXML a quick example: $xmlfile = 'file.xml'; $xml = simplexml_load_file($xml); $images = array(); foreach($xml->pic as $link){ $images[] = $link->image; } Quote Link to comment Share on other sites More sharing options...
MidOhioIT Posted April 28, 2010 Author Share Posted April 28, 2010 thank you for the help. There was a few minor typos but you put me on the right track. I am only posting this code and making that statement because I am marking this as resolved and the next person to read this can have the actual code that worked with no syntax or flaws. Thanks again $xmlfile = 'image_and_link.xml'; $xml = simplexml_load_file($xmlfile); $images = array(); foreach($xml->pic as $link) { $images[] = $link->image; $links[] = $link->link; } Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.