www.webdeveloper.com
Results 1 to 11 of 11

Thread: Faster strpos() ?

  1. #1
    Join Date
    Mar 2008
    Posts
    60

    Question Faster strpos() ?

    I'm trying to write a script that grabs and filters integers from pages under a different domain. It looks like this:

    PHP Code:
    1:   $value file_get_contents("http://sample.url.com");
    2:   $begin_pos strpos($value,"<b>Current Value:</b>");
    3:   $raw_val substr($value,$begin_pos,30);
    4:   $clean_val filter_var($raw_valFILTER_SANITIZE_NUMBER_INT); 
    Which should be easy to interpret.
    1: Reads the source of the target page into a string.
    2: Searches for a string within the source that will indicate where the integer is.
    3: Goes to that spot in the source and returns the next 30 characters which should contain the integer.
    4: Filters the 30 characters for the integer alone.

    The script works as it should, but it's too slow. On top of this, my entire script is written to output around 150 integers the same way. It takes 6-8 minutes to finish.

    Is there faster way to do this, even if it involves another language? Thanks for reading.

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742

  3. #3
    Join Date
    Mar 2008
    Posts
    60
    For instance http://www.sample.url.com:

    <html>
    <body>
    <div id="header"></div>
    <div id="productvalue">
    <b>Current Value:</b> 1,413
    </div>
    </body>
    </html>


    The actual pages I'm trying to pull values from each have 15kb+ source. That may be the reason the script takes so long, strpos() has a lot of source to sift through. I'm not sure.
    Last edited by okendoze; 05-07-2009 at 02:56 PM.

  4. #4
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742

  5. #5
    Join Date
    Mar 2008
    Posts
    60
    I'll have to look that up. Would it be quicker?

  6. #6
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,742

  7. #7
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,399
    Probably the slowest part of all of that is the file_get_contents() of the remote file. Anything we talk about here to optimize parsing/selecting text from it probably will be dealing in milliseconds at most, whereas the HTTP retrieval may be measured in whole seconds.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  8. #8
    Join Date
    Nov 2008
    Posts
    2,477
    Quote Originally Posted by okendoze View Post
    I'll have to look that up. Would it be quicker?
    For info, strpos is MUCH faster than using the regex engine, but listen to NogDog, he's right about this.

  9. #9
    Join Date
    Mar 2008
    Posts
    60
    I see. I was thinking I could write a script to pull the values from the remote pages on its own every hour or so, and have it write the values to a text file on my server. Then I could use this script to pull the values from the text file and it would probably be much faster. But how I would have a script execute on it's own every hour, I don't know.
    Last edited by okendoze; 05-07-2009 at 05:22 PM.

  10. #10
    Join Date
    Nov 2008
    Posts
    2,477
    http://www.google.co.uk/search?hl=en&q=cron+php

    You might also consider using AJAX to send asynchronous requests if you dont care about doing them in a specific order. Should cut the time dramatically.
    Last edited by Mindzai; 05-07-2009 at 05:32 PM.

  11. #11
    Join Date
    Mar 2008
    Posts
    60
    Cron looks like it would do the trick, but I'm on a shared hosting contract that doesn't support it. I've never really studied AJAX, so I'm not sure how it could be used for this purpose. Maybe I'll set this project aside until I'm better prepared. Thanks for your help, bokeh, NogDog, & Mindzai.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles