Thread: preg_match_all
View Single Post
Old 04-04-2008, 11:36 PM   #6 (permalink)
Wildhoney
La Vida es Sueño
Advanced Programmer Top Contributor 
 
Wildhoney's Avatar
 
Join Date: Sep 2007
Location: Oldham
Posts: 2,280
Thanks: 90
Wildhoney is on a distinguished road
Default

I'm sure I wrote a class for this once, but I've wrote it again nevertheless as it's easy enough to write. It can be used as simple as this:

php Code:
$pRetail = new TalkPHP_RetailMeNot('namecheap.com');
$pData = $pRetail->getData();

You would then loop the results like so:

html4strict Code:
<table>
    <tr>
        <th>Code</th>
        <th>Description</th>
        <th>Success Rate</th>
    <tr>
   
    <?php foreach($pData as $pItem): ?>
    <tr>
        <td><?php echo $pItem->code; ?></td>
        <td><?php echo $pItem->description; ?></td>
        <td><?php echo $pItem->success; ?></td>
    </tr>
    <?php endforeach; ?>
   
</table>

...And finally the class itself Enjoy it! Just give TalkPHP.com some credit when the opportunity arises. Please!

php Code:
class TalkPHP_RetailMeNot
{
    private $m_szAddress;
    private $m_aData;
   
    public function __construct($szAddress)
    {
        $this->m_aData = array();
        $this->m_szAddress = sprintf('http://www.retailmenot.com/view/%s', $szAddress);
        $this->parse();
    }
   
    public function getData()
    {
        return (object) $this->m_aData;
    }
   
    public function parse()
    {
        $szContents = file_get_contents($this->m_szAddress);
       
        preg_match_all
        (
            '~<td class="code" id="code.+?"><strong>(.+?)</strong>.*<td class="discount">(.+?)</td>.*<td class="stats"><span class=".+?">(.+?) success rate</span>~im',
            $szContents,
            $aMatches
        );
       
        $iCount = count($aMatches[1]);
       
        for($iIndex = 0; $iIndex < $iCount; $iIndex++)
        {
            $this->m_aData[] = (object) array
            (
                'code'    => $aMatches[1][$iIndex],
                'description'   => $aMatches[2][$iIndex],
                'success'     => $aMatches[3][$iIndex]
            );
        }
    }
}
__________________
The man who comes back through the Door in the Wall will never be quite the same as the man who went out.
Send a message via AIM to Wildhoney Send a message via MSN to Wildhoney Send a message via Yahoo to Wildhoney
Wildhoney is offline  
Reply With Quote