Get product information from html source - regex

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
joukar
Forum Newbie
Posts: 1
Joined: Thu Sep 03, 2015 2:10 am

Get product information from html source - regex

Post by joukar »

When I read HTML source of below link

http://www.dresslink.com/women-candy-co ... 0908.html
I can find below data about the product:

Code: Select all

<script type="text/javascript">
item.stock['ss42356']=[];
DL.item.stock['ss42356']['qty']=56;
DL.item.stock['ss42356']['sku']='SV000837_B';
DL.item.stock['ss42356']['inexistence']=0;
DL.item.stock['ss42356']['down_shelf']=0;
DL.item.stock['ss42356']['procurement_cycle']='8';
DL.item.stock['ss42356']['paid_set']=[];
DL.item.stock['ss42356']['paid_set'].push(35630);
DL.item.color_image['35630']='of7ea7';
DL.item.stock['ss42357']=[];
DL.item.stock['ss42357']['qty']=29;
DL.item.stock['ss42357']['sku']='SV000837_G';
DL.item.stock['ss42357']['inexistence']=0;
DL.item.stock['ss42357']['down_shelf']=0;
DL.item.stock['ss42357']['procurement_cycle']='6';
DL.item.stock['ss42357']['paid_set']=[];
DL.item.stock['ss42357']['paid_set'].push(35631);
DL.item.color_image['35631']='of710e';
DL.item.stock['ss42358']=[];
DL.item.stock['ss42358']['qty']=14;
DL.item.stock['ss42358']['sku']='SV000837_BR';
DL.item.stock['ss42358']['inexistence']=0;
DL.item.stock['ss42358']['down_shelf']=0;
DL.item.stock['ss42358']['procurement_cycle']='17';
DL.item.stock['ss42358']['paid_set']=[];
DL.item.stock['ss42358']['paid_set'].push(35632);
DL.item.color_image['35632']='of77c1';
DL.item.stock['ss42359']=[];
DL.item.stock['ss42359']['qty']=36;
DL.item.stock['ss42359']['sku']='SV000837_O';
DL.item.stock['ss42359']['inexistence']=0;
DL.item.stock['ss42359']['down_shelf']=0;
DL.item.stock['ss42359']['procurement_cycle']='7';
DL.item.stock['ss42359']['paid_set']=[];
DL.item.stock['ss42359']['paid_set'].push(35633);
DL.item.color_image['35633']='of7136';
</script>
I need to know the quantity for each SKU, so I need to produce a simple array containing each SKU name and it's quantity like below

Code: Select all

$a = array( 'SV000837_B' => '56',
            'SV000837_G' => '29',
            'SV000837_BR' => '14',
            'SV000837_O'  => '36',

          );
Please help me write a PHP code using regex and any other method to provide above array.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Get product information from html source - regex

Post by Christopher »

Well, you could preg_match_all() for "qty\'\]=[0-9]*;" and also for "sku\'\]=\'[0-9A-Za-z\_\-]*\';". The two arrays of matches should line up and you can generate your array.
(#10850)
Post Reply