SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: str_replace

  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    str_replace

    I'm allowing smiles in my message board. I've sorted out using javascript in the addition of the smilie in the input form, but not sure how to replace the smilie string with the smilie image when I echo the entry from my MySql database.

    for example..how do i replace using php

    with <img src="images/smilies/smile.gif">
    with <img src="images/smilies/wink.gif">

    ...etc

    I've read up on str_replace in the php manual but can't quite get my head round it and would appreciate an example based on the above info.

    Also, would str_replace be the correct command to use rather than ereg_replace() or preg_replace().

    Many thanks for your time and hope someone can help.

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    PHP Code:
    $smilies = array(
      
    ':)' => '<img src="images/smilies/smile.gif">',
      
    ';)' => '<img src="images/smilies/wink.gif">'
    );

    $string 'hi there :)';
    $string str_replacearray_keys($smilies), array_values($smilies), $string );
    echo 
    $string
    -- lilleman

  3. #3
    gimme the uuuuuuuuuuu duuudie's Avatar
    Join Date
    Feb 2004
    Location
    Switzerland
    Posts
    2,253
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by intrim
    Also, would str_replace be the correct command to use rather than ereg_replace() or preg_replace().
    Hi
    str_replace is definitly what you are looking for. You don't need tricky regexp for what you want to achive... Now if it comes to custom BBCode............

  4. #4
    SitePoint Enthusiast
    Join Date
    Jan 2003
    Posts
    65
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by lilleman
    Try this:

    PHP Code:
    $smilies = array(
    ':)' => '<img src="images/smilies/smile.gif">',
    ';)' => '<img src="images/smilies/wink.gif">'
    );
     
    $string 'hi there :)';
    $string str_replacearray_keys($smilies), array_values($smilies), $string );
    echo 
    $string
    -- lilleman
    Thanks alot lilleman it works perfect. Much Appreciated.


Bookmarks

Posting Permissions

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