Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts

    usage of preg_replace

    how might I use preg_replace to replace a :) with a source to an image? Can someone show an example with more than one smiley combination?

    Thanks
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Look at example 2:

    http://uk.php.net/preg_replace

  • #3
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    **slaps forehead**
    Thanks!
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #4
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    <?php
    $string 
    "Thanks Nightfire! :D";
    $patterns[0] = "/:D/";
    $replacements[0] = "<img src=\"grin.gif\">";
    print 
    preg_replace($patterns$replacements$string);
    ?>
    btw, I did check the manual, I just must have overlooked that example
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #5
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    No problem

  • #6
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Bah, now I get an error
    PHP Code:
    $string strip_tags($_POST['comments']);
    $text[0] = "/:|/";
    $image[0] = "<img src=\"emoticons/longface.gif\">";
    $final preg_replace($text$image$string); // line 24
    print $final
    Error is
    Warning: Compilation failed: missing ) at offset 2 in /home2/gudioco/public_html/implex/GuestBook/add.php on line 24
    Last edited by SDP2006; 11-30-2003 at 08:56 PM.
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #7
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Anyone?
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #8
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,265
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Not sure, but try doing the preg_replace() without any spaces in

    PHP Code:
    $final preg_replace($text,$image,$string); 
    or try

    PHP Code:
    $text[0] = "/:\\|/"
    Can only be something stupid

  • #9
    Regular Coder
    Join Date
    May 2003
    Location
    34° 54' N 82° 13' W
    Posts
    996
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I really don't know, but I fixed it. Here is the code that works
    PHP Code:
    $string $_POST['comments'];
    $nohtml strip_tags($string);

    $patterns[0] = "/:D/";
    $patterns[1] = "/:happy:/";
    $patterns[2] = "/:P/";

    $replacements[2] = "<img src=\"longface.gif\">";
    $replacements[1] = "<img src=\"emoticons/longface.gif\">";
    $replacements[0] = "<img src=\"longface.gif\">";

    $final preg_replace($patterns$replacements$nohtml);
    print 
    $final
    Hmmm. o well, it works
    Stevie Peele
    Neverside IRC Network - irc.veonex.net | tc.tutorialnetwork.org
    #dev - any programming,etc. question
    #design - design discussion and critque
    #central - general chat
    Come join us!

  • #10
    Regular Coder
    Join Date
    Jun 2002
    Location
    Sheffield, UK
    Posts
    552
    Thanks
    0
    Thanked 0 Times in 0 Posts
    it was the :| that was breaking it, since | is part of the regexp syntax, u need to use :/| for the :| smiley, altho i think str_replace would be fine for this usage.
    "To be successful in IT you don't need to know everything - just where to find it in under 30 seconds"

    (Me Me Me Me Me Me Me Me Me)

  • #11
    New Coder
    Join Date
    Oct 2003
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts
    instead of preg, why don't you use str_replace instead... it's less sensitive.. and you don't need all those fancy expression replacements...


  •  

    Posting Permissions

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