treyjeff
asked on
Replacing text in a large string
I have a table that contains a fromText and a toText so that I can "translate" some words submitted in a textarea. I was doing a select on the translate table and in the while I was running a str_replace on the textarea variable $sampleText and replacing phrases. Now, I ran into a few problems. Some of the text that needs to be replaced can be small - for example ur changed to your. However, it would also change four to foyour. So I changed the str_replace to add a space before and after the fromText and that would mostly work unless it followed a newline or was surrouned in quotes.
So I'm wondering what the best way to handle this is...
So I'm wondering what the best way to handle this is...
ASKER CERTIFIED SOLUTION
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
ASKER
TeRReF your solution almost works but throws a preg error.
ASKER
$thriceText = $_POST['thriceText'];
$sql = 'SELECT * FROM translate ORDER BY fromText';
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_assoc($result)
{
$thriceText = str_replace(' ' . $row['fromText'] . ' ', ' ' . $row['toText'] . ' ', $thriceText);
}