Ok first of all. I'll explain what I'm trying to achieve.
This is a little script that will take a post similar to this one, and from a list of cards from a table in the database, compare it and if the words match, then it will add a [card]MATCHED WORD[/card] ([card] tags around the matched word).
Code:
if ( !empty($selected_text) )
{
$sql = "SELECT card_name
FROM " . CARDS_TABLE . "
ORDER BY length(card_name) DESC";
if ( !($result = $db->sql_query($sql)) )
{
message_die(GENERAL_ERROR, 'Could not obtain search results', '', __LINE__, __FILE__, $sql);
}
if ( $row = $db->sql_fetchrow($result) )
{
$i = 0;
do
{
$text_arr[$i] = $row['card_name'];
$selected_text = eregi_replace($text_arr[$i],'[card]'.$i.'[/card]',$selected_text);
$i++;
}
while ( $row = $db->sql_fetchrow($result) );
for($j = 0; $j < $i; $j++)
{
$selected_text = str_replace('[card]'.$j.'[/card]','[card]'.$text_arr[$j].'[/card]',$selected_text);
}
}
$db->sql_freeresult($result);
}
I tested the SQL query and it's not the slow part. Now it's the eregi_replace. I wanted to use str_replace but it's case sensitive and str_ireplace is only available in PHP5 (I'm using PHP4).
So, I need some way to speed up the egeri_replace while loop.
Am I doing this as fast as possible?
Is there any way I could speed this search and replace up?
Can anyone help me?
Sorry if my english is bad.