We help IT Professionals succeed at work.

Replacing text in a large string

treyjeff asked
Last Modified: 2011-09-20
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...
Watch Question


Here is what I'm currently doing

$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);
This one is on us!
(Get your first solution completely free - no credit card required)


TeRReF your solution almost works but throws a preg error.
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.