hi everyone im trying to make my program convert all the text in a text area into sms speak ie great becomes gr8 my program connects to a database and retrives old text = great and newtext = gr8 and compares them to the text area to see if it matches but because the program seperates the words into an array by the spaces words like "be right back" becomes "b right back" instead of "brb" here is my code so far also i have an index page that has the text area on it and send the text via the variabl "smstext"

<?php
include("dbcnx.php");
$phrase = $_GET['smstext'];

$sql="SELECT * FROM sms_keywords ORDER BY keyword";
$result=mysql_query($sql);
$a=0;
while($row=mysql_fetch_array($result) {
$oldtext[$a]=$row['keyword'];
$newtext[$a]=$row['replaceWord'];
$a++;
}

//Separate the provided text into separate words
$textArray=explode(" ",$phrase);
$b=0;
foreach($textArray as $word) {
$i=0;
foreach($oldtext as $checkMatch) {
if($word==$checkMatch){
$textArray[$b]=$newtext[$i];
}
$i++;
}
$b++;
}
//output the whole array
foreach($textArray as $finalWord) {
echo $finalWord." ";
}

?>

    hi there ihave change my code and still having problems im using str_replace and it is working but the problem i have is words like "be right back" get changed to "b right back" because i looks thougth the words and see be and changes it to b here is my code

    <?php
    include("dbcnx.php");
    $phrase = $_GET['smstext'];

    $sql="SELECT * FROM sms_keywords ORDER BY keyword";
    $result=mysql_query($sql);
    $a=0;

    while($row=mysql_fetch_array($result)) {
    $oldtext[$a] = $row['keyword'];
    $newtext[$a] = $row['replaceWord'];
    $finalword = str_replace($oldtext,$newtext,$phrase);
    $a++;
    }
    echo $finalword;

    ?>

      hello i can get it to work if i defne the array my self ie

      $oldtext = array("are","you","be right back")
      $newtext = array("r","u","brb")

      this works but i would like it to jsut get the values from the database

        Try this:

        <?php
        include("dbcnx.php");
        $phrase = $_GET['smstext'];
        
        $sql="SELECT * FROM sms_keywords ORDER BY keyword";
        $result=mysql_query($sql);
        
        while($row=mysql_fetch_array($result)) {
        $oldtext[] = $row['keyword'];
        $newtext[] = $row['replaceWord'];
        }
        $finalword = str_replace($oldtext,$newtext,$phrase);
        echo $finalword;
        ?>
        

          still does the same thing when you type "be right back" you get "b right back"

            It's probably because of the ordering of the replacement.

            It's looking for "be" and changing it to "b"

            Then when it gets to looking for "be right back" it doesn't find it because it is now "b right back".

            You need to make sure that the order of the results from the database are such that the data for "be right back" comes before "be"

            Because you're ordering by 'keyword', "be" comes before "be right back".

            It shouldn't matter what order they are returned from the database apart from this case you so can drop the "ORDER BY" but make sure in the database that "be right back" and its replacement are before "be" and its replacement

              thanks that what the prob was also another prob i have is that a person with the name be gets changed to bn is that hard to solve

                Try making this change:

                $oldtext[] = " {$row['keyword']} "; 
                $newtext[] = " {$row['replaceWord']} "; 

                and u should have your array something like this:

                $oldtext = array(" are "," you "," be right back ");
                $newtext = array(" r "," u "," brb ");

                and "be" from "benny" wont be changed ...

                  Write a Reply...