Str_replace help needed

Discussion in 'PHP' started by baris22, Feb 9, 2007.

  1. #1
    Please help

    I want to use


    str_replace
    PHP:
    for
     $find[] = '1';
      $find[] = '2'; 
      $find[] = '3';
      $find[] = '4';
      $find[] = '5';
      $replace[] = 'audio';
      $replace[] = 'video';
      $replace[] = 'document';
      $replace[] = 'program';
      $replace[] = 'others';
    PHP:
    on
    [type]
    PHP:
    in
    echo "<td align=left bgcolor=$bgcolor id='title'><font face='Verdana' size='2'>$noticia[type]</font></td>"; 
    PHP:
     
    baris22, Feb 9, 2007 IP
  2. Clive

    Clive Web Developer

    Messages:
    4,507
    Likes Received:
    297
    Best Answers:
    0
    Trophy Points:
    250
    #2
    Below is a scheme that might work for you
    (you can also read about str_replace here):
    $text = '...';
    
    $text = str_replace("1", "audio", $text);
    $text = str_replace("2", "video", $text);
    $text = str_replace("3", "document", $text);
    $text = str_replace("4", "document", $text);
    $text = str_replace("5", "document", $text);
    
    echo $text;
    
    PHP:
     
    Clive, Feb 9, 2007 IP
  3. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #3
    No need to call the function multiple times. Baris can use his arrays.

    
    
    $text = str_replace($find, $replace, $text);
    
    PHP:
     
    nico_swd, Feb 10, 2007 IP
  4. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #4
    But I do not know how to use

    $text = str_replace($find, $replace, $text);
    PHP:
    on

    echo "<td align=left bgcolor=$bgcolor id='title'><font face='Verdana' size='2'>$noticia[type]</font></td>"; 
    PHP:


    I used str_replace on another page. It worked. The code was

    echo "<td>" . str_replace($find, $replace, $row['type']) .  "</td>";
    PHP:
    I want to do same thing here

    
    echo "<td align=left bgcolor=$bgcolor id='title'><font face='Verdana' size='2'>$noticia[type]</font></td>"; 
    
    PHP:
    But I do not know how to do.
     
    baris22, Feb 10, 2007 IP
  5. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #5
    echo "<td align=left bgcolor=$bgcolor id='title'><font face='Verdana' size='2'>".str_replace($find, $replace, $noticia['type'])."</font></td>";
    PHP:
    It should work for you.
     
    designcode, Feb 10, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    I see you want to replace numbers with words? Where do there numbers come from? The database row? If $noticia[type] is only a number (1 to 5), I'd do something like this.

    
    
    $replace = array(
         1 => "audio",
         2 => "video",
         3 => "document",
         4 => "document",
         5 => "document"
    );
    
    echo " [snip] <font face='Verdana' size='2'>". $replace[$noticia['type']] ."</font> [snip]";
    
    PHP:
     
    nico_swd, Feb 10, 2007 IP
  7. baris22

    baris22 Active Member

    Messages:
    543
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #7

    Thank you very much.
    It worked.


    Thanks again/
     
    baris22, Feb 10, 2007 IP
  8. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #8
    Baris, I am happy that it worked for you :)
     
    designcode, Feb 10, 2007 IP