Found this simple but useful str_replace function on php.net:
Code:
function str_replace_assoc($array,$string){
$from_array = array();
$to_array = array();
foreach ($array as $k => $v){
$from_array[] = $k;
$to_array[] = $v;
}
return str_ireplace($from_array,$to_array,$string);
}
$replace = array(
'i' => 'I',
'we' => 'We',
'apple' => 'Carrot',
);
$replace_string = str_replace_assoc($replace,$string);
///////////////////////////////////////////////////////////////
I was trying to extend it so that the key/vale pars will be selection from a database and create the array. I am hoever having troubles getting it done.
Here is what i have so far:
Code:
//post the message
mysql_select_db($database_conn, $conn);
$gw = "select * from table";
$r_gw = mysql_query($gw,$conn) or die("the gw query complained: ".mysql_error());
$replace = array();
while($row_gw = mysql_fetch_assoc($r_gw)) $replace[] = $row['name_cmp'] => $row_gw['translation'];
I am getting a parse error where the while loop is founded.
I could just go with the setup as is where all the key/vale pairs are in a file. But i wanted something that i could easily update on a database. I am not sure if this is a faster method though compare to the flat file.