我担心速度,因为 do 循环可能必须运行 15-20 次。在每个循环中,我搜索和替换几次,每次从 mysql 表中选择一次。我只是希望它尽可能快。
$text = file_get_contents('texts/phir-mohabbat.txt');
preg_match_all('/\S+@/', $text, $words);
preg_match_all('/@\S+/', $text, $lexs);
$count = count($words[0]);
$i = 0;
do {
$bad = array('@', ',', '।', '?');
$word = str_replace('@', '', $words[0][$i]);
$lex = str_replace($bad, '', $lexs[0][$i]);
if($lex == '#') {$lex = $word;}
$get_def = mysqli_query($con,"SELECT * FROM hindi_dictionary WHERE lex = '$lex'");
while($row = mysqli_fetch_array($get_def)) {
$def = $row['def'];
}
$find = array($word.'@'.$lex, $word.'@#');
$replace = '<span class = "word-info" onmouseover="show_info(\''.$lex.' - '.$def.'\',\''.$word.'\');">'.$word.'</span>';
$text = str_replace($find, $replace, $text);
$i++;
} while ($i < $count);
echo nl2br($text);