So, I figured to translate words, preg_replace would do.
The idea of the script I'm trying to create is that you can insert it into an HTML page and if anybody wants to translate it, they can append ?language=(two-letter language code) to the URL and whatever is inside the dictionary will be translated.
It works with one word, but I want to make it work with multiple words, and loops aren't really helping.
<?php
$cxn = mysqli_connect("localhost","[hidden]","[hidden]","cgi");
$sql = "SELECT * FROM dictionary";
$result = mysqli_query($cxn,$sql);
$row = mysqli_fetch_assoc($result);
$max = mysqli_num_rows($result);
$i = 0;
for($i=0;$i>$max;++$i)
{
$string[$i] = $rows['first_en'].$rows['restofword_en'].$rows['last_en'];
echo $string[$i];
}
if($_GET['language'] == $row['tolanguage'])
{
$firste = $row['first_en'];
$laste = $row['last_en'];
$reste = $row['restofword_en'];
$firstn = $row['first'];
$lastn = $row['last'];
$restn = $row['restofword'];
$regex = "#^$firste($reste)$laste$#e";
$replacement = "('{$firstn}{$restn}{$lastn}')";
for($i2=0;$i2>$max;++$i2)
{
$output[$i2] = preg_replace($regex,$replacement,$string[$i2]);
echo $output[$i2];
}
}
else
{
echo $string;
}
?>
Works great with one row like this:
<?php
$cxn = mysqli_connect("localhost","[hidden]","[hidden]","cgi");
$sql = "SELECT * FROM dictionary";
$result = mysqli_query($cxn,$sql);
$row = mysqli_fetch_assoc($result);
$string = $rows['first_en'].$rows['restofword_en'].$rows['last_en'];
if($_GET['language'] == $row['tolanguage'])
{
$firste = $row['first_en'];
$laste = $row['last_en'];
$reste = $row['restofword_en'];
$firstn = $row['first'];
$lastn = $row['last'];
$restn = $row['restofword'];
$regex = "#^$firste($reste)$laste$#e";
$replacement = "('{$firstn}{$restn}{$lastn}')";
$output = preg_replace($regex,$replacement,$string);
echo $output;
}
else
{
echo $string;
}
?>
Is there any way to loop preg_replace so that if I have multiple words all of them will be translated?
Attachments: Table.