Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

php curl regular expression question

Options
  • 22-12-2007 9:34pm
    #1
    Registered Users Posts: 648 ✭✭✭


    hi

    i need to get a specific value from a webpage im getting via php and curl but im having issues

    suppose i have the content of a whole page in a variable called $content

    suppose i want the price for a table as below :

    <td class="listprice">$50.00 </td>

    how do i read the actual price into a variable ?
    ie say get the content in between '<td class="listprice">' and '</td>' ?

    thanks


Comments

  • Registered Users Posts: 3,594 ✭✭✭forbairt




  • Closed Accounts Posts: 1,200 ✭✭✭louie


    [php]
    preg_match('/<td\sclass=[\'\"]listprice[\'\"]>(.*?)<\/td>/si',$str,$matches);
    [/php]

    give this a try


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    thats great louie

    however supposing im going to be using this alot is there any way to turn my search strings into regular expressions. for example something like this

    function change2reg($str){

    TURN $str into regular expression $ret

    return $ret;
    }

    $termtochangeb4='<td class="listprice">';

    $termtochangeaR=change2reg($str)




    preg_match('/$termtochangeaR(.*?)<\/td>/si',$str,$matches);


    u follow ?


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    did you try it?
    Are you getting an error?


  • Registered Users Posts: 3,594 ✭✭✭forbairt


    thats great louie

    however supposing im going to be using this alot is there any way to turn my search strings into regular expressions. for example something like this

    function change2reg($str){

    TURN $str into regular expression $ret

    return $ret;
    }

    $termtochangeb4='<td class="listprice">';

    $termtochangeaR=change2reg($str)




    preg_match('/$termtochangeaR(.*?)<\/td>/si',$str,$matches);


    u follow ?

    '$termtochangeaR' won't be recognized it'd need to be in "'s

    you'll also want to addslashes to the search term

    you'll run into a problem if you've a nested table as well ...


  • Advertisement
  • Registered Users Posts: 2,157 ✭✭✭Serbian


    I'm really questioning why you need to create Regular Expressions on the fly. Can you give us any more info on what you are actually trying to do?


  • Registered Users Posts: 648 ✭✭✭ChicoMendez


    Serbian wrote: »
    I'm really questioning why you need to create Regular Expressions on the fly. Can you give us any more info on what you are actually trying to do?


    ok bascially i want to grab product info from certain suppliers sites - details like product code, price, description etc etc etc

    basically i would create an array of links to product pages for supplier 1 and my code would run through the array calling a function that would grab the data from each page and write it to a db.

    so why do i want to be able to create the regular expressions on the fly - so as basically when it comes to supplier 2,3,4,5,6,7 etc then all i have to do is pass in the html code before and after the required info (eg price) and the html code would be converted to a regular expression on the fly therefore saving me the hassle of creating regular expressions for each supplier

    u follow ?


  • Closed Accounts Posts: 12,382 ✭✭✭✭AARRRGH


    I do a lot of spidering stuff.

    You need to loop through each page of results, e.g.

    for each supplier
    for each page of that suppliers products

    I normally just match a group of HTML on the page, e.g. first product to last product, and then split the products up using a regex. The HTML should only change slightly between each product, and generally the bits which have changed are the bits which you'd want to save anyway, such as the product title, code, etc.

    I really recommend you read the O'Reilly Regular Expressions book.


Advertisement