Jump to content

Explode Help


Sleeper

Recommended Posts

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];

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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>';
}

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.