Slow script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Slow script

Post by ILoveJackDaniels »

Hiya, I've written this script to check the spelling of text sent from a form as $texttocheck. It runs really slowly, taking about a quarter of a second to check each word. Can anyone spot anything slowing this down badly, or suggest any ways I could change it to make it faster? Apologies in advance for the incompetent coding....

Code: Select all

while (preg_match("/  /",$texttocheck)) {
$texttocheck=str_replace("  "," ",$texttocheck);
}
while (preg_match("/\n/",$texttocheck)) {
$texttocheck=str_replace("\n"," <br> ",$texttocheck);
&#125;

$words_array=explode(" ",$texttocheck);
$wordnumber="0";
$link=@mysql_connect("localhost", "******", "*******");
@mysql_select_db("*******");
while ($words_array&#1111;$wordnumber]!="") &#123;
$word=$words_array&#1111;$wordnumber];
if ($word=="<br>") &#123; 
	echo " <br> ";
	$wordnumber++;
	continue;
&#125;
if (preg_match("/-/",$word)) &#123;
$words=explode("-",$word);
$number=0;
while ($words&#1111;$number]!="") &#123;
$wordtocheck=eregi_replace("&#1111;^a-z]","",$words&#1111;$number]);
$fromdb=@mysql_query("SELECT id FROM language WHERE word='$wordtocheck'");
$number2=@mysql_num_rows($fromdb);
if ($number2=="0") &#123;
echo "<input type="text" size="8" STYLE="background:#ffffff; color:#000000; border-width:1px; border-color:#cc0000; border-style:ridge" value="$words&#1111;$number]" align="absmiddle">";
&#125; else &#123;
echo "$words&#1111;$number]";
&#125;
$number++;
if ($words&#1111;$number]!="") &#123; echo "-"; &#125;
&#125;
echo " ";
&#125; else &#123;
$wordtocheck=eregi_replace("&#1111;^a-z]","",$word);
$fromdb=@mysql_query("SELECT id FROM language WHERE word='$wordtocheck'");
$number=@mysql_num_rows($fromdb);
if ($number=="0") &#123;
echo "<input type="text" size="8" STYLE="background:#ffffff; color:#000000; border-width:1px; border-color:#cc0000; border-style:ridge" value="$word" align="absmiddle"> ";
&#125; else &#123;
echo "$word ";
&#125;
&#125;
$wordnumber++;
&#125;
@mysql_close($link);
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

Yup, pretty much, you are checking each word against a database, and using preg_* and ereg_* functions in loops. That will chew up a lot of time.

I would suggest one of several things.

First, stick to preg_* functions, they are faster.

Next, is their anyways you can cut down on the number of MySQL queries?

Have you considered optimizing your table? I don't know what the table structure looks like, what indexes you have, or what keys.

This might help.
User avatar
ILoveJackDaniels
Forum Commoner
Posts: 43
Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK

Post by ILoveJackDaniels »

Thanks for the suggestions Jason. Unfortunately I have no idea how I would go about reducing the number of MySQL queries, other than listing common words (and, the, I, there, monkey etc) and having them skipped... Any ideas?

The preg functions seem to have sped it up a little too, and nothing can be changed really in the database, but I'm still playing around with things to change...
MattF
Forum Contributor
Posts: 225
Joined: Sun May 19, 2002 9:58 am
Location: Sussex, UK

Post by MattF »

preg_replace accepts arrays of things to search for and replace, so why not try getting an array of all mis-spellings and their correct forms, eg

"functoins" => "functions" and run the whole text through that, it might take a while.....
jason
Site Admin
Posts: 1767
Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:

Post by jason »

You might want to consider the aspell, or pspell extensions to PHP.
Post Reply