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
ILoveJackDaniels
Forum Commoner
Posts: 43 Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK
Post
by ILoveJackDaniels » Mon May 27, 2002 4:57 am
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);
}
$words_array=explode(" ",$texttocheck);
$wordnumber="0";
$link=@mysql_connect("localhost", "******", "*******");
@mysql_select_db("*******");
while ($words_arrayї$wordnumber]!="") {
$word=$words_arrayї$wordnumber];
if ($word=="<br>") {
echo " <br> ";
$wordnumber++;
continue;
}
if (preg_match("/-/",$word)) {
$words=explode("-",$word);
$number=0;
while ($wordsї$number]!="") {
$wordtocheck=eregi_replace("ї^a-z]","",$wordsї$number]);
$fromdb=@mysql_query("SELECT id FROM language WHERE word='$wordtocheck'");
$number2=@mysql_num_rows($fromdb);
if ($number2=="0") {
echo "<input type="text" size="8" STYLE="background:#ffffff; color:#000000; border-width:1px; border-color:#cc0000; border-style:ridge" value="$wordsї$number]" align="absmiddle">";
} else {
echo "$wordsї$number]";
}
$number++;
if ($wordsї$number]!="") { echo "-"; }
}
echo " ";
} else {
$wordtocheck=eregi_replace("ї^a-z]","",$word);
$fromdb=@mysql_query("SELECT id FROM language WHERE word='$wordtocheck'");
$number=@mysql_num_rows($fromdb);
if ($number=="0") {
echo "<input type="text" size="8" STYLE="background:#ffffff; color:#000000; border-width:1px; border-color:#cc0000; border-style:ridge" value="$word" align="absmiddle"> ";
} else {
echo "$word ";
}
}
$wordnumber++;
}
@mysql_close($link);
jason
Site Admin
Posts: 1767 Joined: Thu Apr 18, 2002 3:14 pm
Location: Montreal, CA
Contact:
Post
by jason » Mon May 27, 2002 6:23 am
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.
ILoveJackDaniels
Forum Commoner
Posts: 43 Joined: Mon May 20, 2002 8:18 am
Location: Brighton, UK
Post
by ILoveJackDaniels » Tue May 28, 2002 7:24 am
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 » Tue May 28, 2002 11:08 am
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 » Tue May 28, 2002 11:13 am
You might want to consider the aspell, or pspell extensions to PHP.