Solved

Can a regex be conditional (pickup this or that)

Posted on 2010-11-29
4
364 Views
Last Modified: 2012-05-10
I'm building a small application to allow me to gather sell price on ebay, I divided the script in 5 different preg_match_all section, one for each of the 5 data I want to pull

1) title
2) item number
3) bids
4) price
5) date

with the help of other Experts here I've been able to filter only the sold items, everything work pretty good but there are a few glitches and my script needs some fine tuning, here is an example;

             preg_match_all("'<td class=\"prc bidsold g-b\">(.*?)</td>'si", $source, $price_arr);
                         
         foreach($price_arr[1] as $price)
           {
                  echo "<tr><td><input type=\"text\" name=\"price[]\" size=\"10\" value=\"".$price."\"></td>";
           }
 
This script allows me to get the sold price of an item, the line to scrape look like this

<td class=\"bids\"><div class=\"bin1\">5 Bids</div><span class=\"sold\">Sold</span></td><td class=\"prc bidsold g-b\">$300.00</td><td class=\"tme  rt\">

But if the sellers offer free shipping than the line look like this

<td class=\"bids\"><div class=\"bin1\">1 Bid</div><span class=\"sold\">Sold</span></td><td class=\"prc\"><div class=\"bidsold g-b\">$10.76</div><span class=\"tfsp\">Free shipping</span></td><td class=\"tme  rt\">

As you can see the "prc bidsold g-b" that I used to find inside a td tag is now split, the "prc" part is still inside a td tag but the "bidsold g-b" is now inside a div tag.

So the question is how can I modify the preg_match_all regex to pickup both instance, I tried different approach and the best I could get was an empty cell in my return table, as it is I don't even get anything return if the seller offered free shipping.

If at all possible I'd like a detail explanation because I have a few other similar situation that I need to address (like if the title is bold I don't pick it up, but that is another question).

Thanks
0
Comment
Question by:gamebits
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 34235666
The simplest solution would probably be to just add a new condition for the separate scenario, using a vertical bar ( OR ) to separate the conditions. For the following, I just created a pattern to match the "free shipping" condition. It is pretty much the same as what you had before, just with <div> and the altered class attributes.
preg_match_all("'(?:<td class=\"prc bidsold g-b\">|<div class=\"bidsold g-b\">)(.*?)(?:</td>|</div>)'si", $source, $price_arr);

Open in new window

0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34235671
P.S.

The (?: ... ) is a non-capturing grouping construct used to provide an internal boundary for the OR condition.
0
 
LVL 28

Author Comment

by:gamebits
ID: 34235696
Awesome, works perfect, are you up for more points, I can post 2 mores questions similar to this in the mean time you may want to have a look at my other question still open.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 34235700
I'm always game to help out  :)
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

622 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question