pepsi_max2k Posted May 21, 2009 Share Posted May 21, 2009 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... Quote Link to comment https://forums.phpfreaks.com/topic/159170-best-practice-when-parsing-how-to-speed-it-up/ Share on other sites More sharing options...
CarbonCopy Posted May 21, 2009 Share Posted May 21, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/159170-best-practice-when-parsing-how-to-speed-it-up/#findComment-839462 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.