Results 1 to 6 of 6

Thread: extending a str_replace() sample script

  1. #1
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default extending a str_replace() sample script

    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.
    Last edited by leocrawf; Mar 24, 2010 at 10:32 PM.
    "...men are that they might have joy."
    Nephi

  2. #2
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    this won't work;

    $replace[] = $row['name_cmp'] => $row_gw['translation'];

    you can only use the => operator in a array() statment.

    I assume you want to do this;

    $replace[ $row['name_cmp'] ] = $row_gw['translation'];

    that will create an array similar to the one in your example. Understanding how to maniplate array in php can be tricky but are extrodinarily useful thing to know. I am sure there are simpler ways to do this but the code should work now. have a good day.

  3. #3
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    Thanks for responding. I have tried your suggestion, and while i did not get an error message, it however does not change the value when it should. Nothing happended.
    Last edited by leocrawf; Mar 29, 2010 at 09:43 AM.
    "...men are that they might have joy."
    Nephi

  4. #4
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    oh ok, there was a error in your code; Fixed below;

    $replace[ $row_gw['name_cmp'] ] = $row_gw['translation'];

    see the $row variable should be $row_gw. I often avoid getting this error by using standard variable names but try that instead. You should learn how to improve you debugging skills.

  5. #5
    Join Date
    May 2004
    Posts
    530
    Rep Power
    0

    Default

    O yes! I had seen the error and fixed it as above but i still did not get it to pass the $row_gw['translation'] value. Are you sure that:
    Code:
    $replace[ $row_gw['name_cmp'] ] = $row_gw['translation']
    is equivalent to the:
    Code:
    'key' => 'value',
    "...men are that they might have joy."
    Nephi

  6. #6
    Join Date
    Feb 2003
    Posts
    3,184
    Rep Power
    0

    Default

    Yes I'm sure. What you need to do is put print_r($row_gw) statements in the while loop and put a print_r($replace); after the while loop so that you can DEBUG you code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •