Jump to content

Recommended Posts

Hey all, I have a question about parsing HTML pages. I have a script that grabs one, stores it as $result, then runs numerous preg_match_all commands on $result (without changing it).

 

First I run a preg_match to find whether there's any values worth further parsing (looks for a single line). Then I run one preg_match_all to find all negative occurances of a value, then another preg_match_all to find all positive results of one type, if non are found then again to find them of a different type, then again to find them of a third type if there's none of a second.

 

That's one preg_match and up to four preg_match_alls on the same page, much of which contains data that's irrelevant to the parsing.

 

So I'm just wondering whether I should run a preg_replace to strip half of the text in $result away, in the hope it'll speed up the latter preg_match_alls? Or is the result not gonna be worth it - maybe preg_replace will take up so much extra time, I may as well leave it as it is?

 

 

 

Oh and while I'm here - if statements. Is using lots really gonna cause any speed issues? I have a few things like this in my code:

 

 if ($type=="Game") {
        // Alter search url based on console.
    	if ($console=="Xbox") {
    		do something
    	} elseif ($console=="PS1") {
    		do something else
    	} elseif ($console=="PS2") {
    		do something else
    	} elseif ($console=="PS3") {
    		do something else
    	} elseif ($console=="GameCube") {
    		do something else
    	} elseif ($console=="Xbox 360") {
    		do something else
    	} elseif ($console=="Wii") { 
    	    do something else
        } elseif ($console=="Nintendo DS") { 
    	    do something else
        }
    } elseif ($type=="Accessory") {
    	if ($console=="PS1") {
    		do something else
    	} elseif ($console=="PS2") {
            do something else
        } else {
    		do something else
    	} 
} else {
       do something else
}

 

Seems a bit, erm... slow to read though. Just seems there'd be a quicker way...

http://www.developertutorials.com/tutorials/php/displaying-load-time-php-050620/page1.html

 

See what the load time is. It may not be the script that's slow

 

preg_replace shouldn't make any noticeable difference with such a small number of preg_match commands.

 

For the if statements, a switch statement may be faster with a large number of options. I'll go check

EDIT: Switch statements are twice as fast with integer conditions, but for strings, there is no noticable difference.

 

Remember, Google is our friend.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.