Sleeper Posted August 2, 2011 Share Posted August 2, 2011 If the variable is outputted at shown below... <p><a href="link"><img src="image" /></a></p> How can I explode this to just get link and image as new variables? Quote Link to comment Share on other sites More sharing options...
Maq Posted August 2, 2011 Share Posted August 2, 2011 You probably don't want to use explode for this task. Rather use XPath or Regex. Quote Link to comment Share on other sites More sharing options...
Sleeper Posted August 2, 2011 Author Share Posted August 2, 2011 I'm not failure with that. can you provide an example? Quote Link to comment Share on other sites More sharing options...
WebStyles Posted August 2, 2011 Share Posted August 2, 2011 if you're sure that is the exact output, you can do something like this: (although regex is a better way to go) $var = '<p><a href="link"><img src="image" /></a></p>'; $var = str_replace('<p><a href="','',$var); $var = str_replace('"><img src="',':',$var); $var = str_replace('" /></a></p>','',$var); $parts = explode(":",$var); $url = $parts[0]; $image = $parts[1]; Quote Link to comment Share on other sites More sharing options...
Sleeper Posted August 2, 2011 Author Share Posted August 2, 2011 I must be doing something wrong. I'm using a script that pulls in xml data called rss2html Inside this script I have $itemdes = "~~~ItemDescription~~~"; $var = $itemdes; $var = str_replace('<p><a href="','',$var); $var = str_replace('"><img src="',':',$var); $var = str_replace('" /></a></p>','',$var); $parts = explode(":",$var); $url = $parts[0]; $cimage = $parts[1]; Now the cimage var should be pulling out the url and no parts after 0 work. The 0 is all the coding not being fragmented up. But if I have the cimage = itemdes it shows all the coding on the page. I'm thinking that its not loading the itemdec var until it figures out what it is and after that its not active to separate because the page load is over now? So that its just trying to separate nothing cause its not figured out what it is yet. Is that the case? Quote Link to comment Share on other sites More sharing options...
xyph Posted August 2, 2011 Share Posted August 2, 2011 Use an XML parser!!! I can show you how easy it is. Just give us a link to the XML file you're reading, or view the source and copy and paste it EXACTLY enclosed in the [ code ] tags. Quote Link to comment Share on other sites More sharing options...
Sleeper Posted August 3, 2011 Author Share Posted August 3, 2011 The info is being pulled from http://www.rollingstone.com/siteServices/rss/musicNewsAndFeature Quote Link to comment Share on other sites More sharing options...
xyph Posted August 3, 2011 Share Posted August 3, 2011 I see how everything is formatted now. Well, sadly we're going to have to make use of a little RegEx. This is a little uglier than most of your XML parsing should be, due to an external namespace mucking things up, but here we go <?php $xml = simplexml_load_file( 'http://www.rollingstone.com/siteServices/rss/musicNewsAndFeature' ); //$xml = new SimpleXMLElement(); // Because the information we want is stored in another namespace, we have // to use that namespace to access it's data. // First, we loop through the <item>s in <channel> foreach( $xml->channel->item as $item ) { // We can then grab the nodes belonging to the external namespace $content = $item->children( 'http://purl.org/rss/1.0/modules/content/' ); // $content->encoded now holds the content we want // Because the XML is generated, we can assume this will never change // and use a simple regex to match. // match '<a href="' followed by anything that's not a ", followed by // '"><img src="', followed by anything that's not a " preg_match( '/<a href="([^"]+)"><img src="([^"]+)/', $content->encoded, $matches ); echo '<p>The URL is: '.$matches[1].'<br>'; echo 'The IMG is: '.$matches[2].'</p>'; } ?> 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.