开发者

translator in php and mysql

ok i make this one but i have 83000 words in mysql database when i execute this script it will take too much time and some time it not runs. i think this script match every title in mysql database wather it is in the $row['full_story'] or not. so this make the opreation unusable if there is any method i can make this process faster ? or it just match those titles which are used $row['full_story'] code is below

$user_name = "root";
    $password = "";
    $database = "salar";
    $server = "127.0.0.1";
$db_handle = mysql_connect($server, $user_name, $password);
$db_found = mysql_select_db($database, $db_handle);
if ($db_found) {
$SQL = "SET NAMES 'utf8'";
    mysql_query($SQL);
$SQL = "SELECT * FROM dl开发者_如何学Ce_mylinks ORDER BY LENGTH( title ) DESC";
$result = mysql_query($SQL);
while ($db_field = mysql_fetch_assoc($result)) {
$row['full_story'] = str_replace ($db_field['title'],"<a href=\"?newsid=" . $db_field['id'] . "\">" . $db_field['title'] . "</a>" ,$row['full_story']);
$row['short_story'] = str_replace ($db_field['title'],"<a href=\"?newsid=" . $db_field['id'] . "\">" . $db_field['title'] . "</a>" ,$row['short_story']);
}
$mydata =$row['short_story'] .  $row['full_story'];


mysql_close($db_handle);

}
else {
print "Database NOT Found ";
mysql_close($db_handle);
}


You'll want to iterate through each row in the table in a loop.

Example:

$query = mysql_query("SELECT * FROM `table`");

while($row = mysql_fetch_object($query)){
    $mydata = str_replace($row->word,$row->meaning,$mydata);
}


Be careful with str_replace, If you want to replace 'distance' per 'thistance' and after it, replaces 'this' by 'dis' you get 'distance' again (it's a word game, an aproximation to the issue)

Use preg_replace.

Create two arrays (zend framework fetchCol or your favorite lib)

$aWords = $zfDb->fetchCol('select words from table');

$aMeans = $zfDb->fetchCol('select means from table');

And

$mydata = preg_replace($aWords, $aMeans, $mydata);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜