Jump to content

Having trouble with preg_match !! Please Help


guvnoruk

Recommended Posts

I am a complete newbie with PHP. but i am making progress.

 

So this is how far i got. I am trying to reformat an xml feed to get it working with my flash widget.

 

So i am using the orginal feed using PHP to format it and then use it. I used "getElementsByTagName" and formatted all what i wanted but now i just stuck with one thing. How to code so that i can get the image url from the xml feed.

 

The problem is the img url is not something i can get by "getElementsByTagName" because its inside a TAG: i dunno if i make sense but here is the xml feed:

 

<item>
<title><![CDATA[sEXY UNIQUE PINK GOGO TOP SHORT WRAP AROUND 6  8 10 12]]></title>
<link><![CDATA[http://cgi.ebay.co.uk/SEXY-UNIQUE-PINK-GOGO-TOP-SHORT-WRAP-AROUND-6-8-10-12_W0QQitemZ380126183045QQcmdZViewItemQQssPageNameZRSS:B:SILF:GB:101]]></link>
<description><![CDATA[<table border="0" cellpadding="8"><tr><td><a href="http://cgi.ebay.co.uk/SEXY-UNIQUE-PINK-GOGO-TOP-SHORT-WRAP-AROUND-6-8-10-12_W0QQitemZ380126183045QQcmdZViewItemQQssPageNameZRSS:B:SILF:GB:102"><img border="0" src="http://thumbs.ebaystatic.com/pict/380126183045_0.jpg"></a></td><td><strong>&#163;7.99</strong><br /> End Date: Sunday Jun-21-2009 18:07:40 BST<br />Buy It Now for only: &#163;7.99<br /><a href="http://cgi.ebay.co.uk/SEXY-UNIQUE-PINK-GOGO-TOP-SHORT-WRAP-AROUND-6-8-10-12_W0QQitemZ380126183045QQcmdZViewItemQQssPageNameZRSS:B:SILF:GB:105">Buy it now</a> | <a href="http://cgi1.ebay.co.uk/ws/eBayISAPI.dll?MfcISAPICommand=MakeTrack&item=380126183045&ssPageName=RSS:B:SILF:GB:104">Add to watch list</a></td></tr></table>]]></description>
<guid isPermaLink="false"><![CDATA[http://cgi.ebay.co.uk/SEXY-UNIQUE-PINK-GOGO-TOP-SHORT-WRAP-AROUND-6-8-10-12_W0QQitemZ380126183045QQcmdZViewItemQQssPageNameZRSS:B:SILF:GB:101]]></guid>
<pubDate>Sat, 23 May 2009 02:02:41 BST</pubDate>
<rx:BuyItNowPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">799</rx:BuyItNowPrice>
<rx:CurrentPrice xmlns:rx="urn:ebay:apis:eBLBaseComponents">799</rx:CurrentPrice>

<rx:EndTime xmlns:rx="urn:ebay:apis:eBLBaseComponents">1245604060000</rx:EndTime>
<rx:Category xmlns:rx="urn:ebay:apis:eBLBaseComponents"><![CDATA[Women's Clothing]]></rx:Category>
<rx:AuctionType xmlns:rx="urn:ebay:apis:eBLBaseComponents">Buy It Now</rx:AuctionType>
<rx:ItemCharacteristic xmlns:rx="urn:ebay:apis:eBLBaseComponents">Listed with PayPal</rx:ItemCharacteristic>
</item>

 

 

Now This is the url i need to specify src="http://thumbs.ebaystatic.com/pict/380126183045_0.jpg, which is under the description tag. I need the value of this URL as a seprate TAG

 

so that i can create my feed for the flash widget.

 

The PHP Is as follows:

 

<?php
$xml = new DomDocument;
$xml -> load("http://rss.api.ebay.com/ws/rssapi?FeedName=StoreItems&siteId=3&language=en-GB&output=RSS20&storeId=116907097");
$str = "";
//get elements from "<channel>"
$channel=$xml->getElementsByTagName('channel')->item(0);
$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

//output elements from "<channel>"
$str .= "<p><strong><a href='". $channel_link."'>".$channel_title."</a></strong>";
$str .= "<br />";
$str .= $channel_desc . "</p>" ;

$node=$xml->getElementsByTagName('item');
$i=0;
$str .= "<ul>";
$str .= "<list>"."\n";

foreach($node as $x){
$item_title=$x->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;
$item_link=$x->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;
$item_desc=$x->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;
$item_date=$x->getElementsByTagName('pubDate')->item(0)->childNodes->item(0)->nodeValue;
$item_image=$x->preg_match('/src=['"]?([^'" >]+)['" >]/',$item_desc,$result);

//$str .= "<li><a href='".$item_link."'>".$item_title ."</a></li><ul><li>";
//$str .= $item_desc . "</li></ul>";
//$str .="<list>"."\n";
$str .="<item>"."\n";
$str .="<date>".$item_date."</date>"."\n";
$str .="<headline>".$item_title."</headline>"."\n";
$str .="<image>".$item_image."</image>"."\n";
  $str .="<news_text><![CDATA[".$item_desc."]]></news_text></item>"."\n";
$i++;
}
//$str .= "</ul>";
$str .= "</list>";
echo $str;
?>

 

Please guys dont laugh :-) but i thought if i use $item_image=$x->preg_match('/src=[\'"]?([^\'" >]+)[\'" >]/',$item_desc,$result);, it might just work but hey it doesnt dunno what to use to get this working .

 

I would be really greateful if someone can point me to the right direction.

 

So in short i want to extract a URL which is under the description TAG for everything in the array. I dunno if i make sense but i think i tried my best.

 

Regards

 

Guvnor

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.