[PHP]A way of shortening this huuuge code :P

Results 1 to 7 of 7
  1. #1
    The Gamma..? EliteGM is offline
    Grand MasterRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    [PHP]A way of shortening this huuuge code :P

    This is the code I use for smileys, and I'd like to shorten it:
    PHP Code:
            $smileys = array('01''02''03''04''05''06''07''08''09''10');
            
            
    $text str_replace(":)""<img src='images/smileys/"$smileys[0] .".gif' />"$text);
            
    $text str_replace(":-)""<img src='images/smileys/"$smileys[0] .".gif' />"$text);
            
    $text str_replace("=)""<img src='images/smileys/"$smileys[0] .".gif' />"$text);
            
            
    $text str_replace(":D""<img src='images/smileys/"$smileys[1] .".gif' />"$text);
            
    $text str_replace(":d""<img src='images/smileys/"$smileys[1] .".gif' />"$text);
            
    $text str_replace("=D""<img src='images/smileys/"$smileys[1] .".gif' />"$text);
            
    $text str_replace("=d""<img src='images/smileys/"$smileys[1] .".gif' />"$text);
            
    $text str_replace(":-D""<img src='images/smileys/"$smileys[1] .".gif' />"$text);
            
    $text str_replace(":-d""<img src='images/smileys/"$smileys[1] .".gif' />"$text);
            
            
    $text str_replace(":O""<img src='images/smileys/"$smileys[2] .".gif' />"$text);
            
    $text str_replace(":o""<img src='images/smileys/"$smileys[2] .".gif' />"$text);
            
    $text str_replace("=O""<img src='images/smileys/"$smileys[2] .".gif' />"$text);
            
    $text str_replace("=o""<img src='images/smileys/"$smileys[2] .".gif' />"$text);
            
    $text str_replace(":-O""<img src='images/smileys/"$smileys[2] .".gif' />"$text);
            
    $text str_replace(":-o""<img src='images/smileys/"$smileys[2] .".gif' />"$text);
            
            
    $text str_replace(":p""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace(":P""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace(";p""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace(";P""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace("=p""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace("=P""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace(":-p""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
    $text str_replace(":-P""<img src='images/smileys/"$smileys[3] .".gif' />"$text);
            
            
    $text str_replace(":(""<img src='images/smileys/"$smileys[4] .".gif' />"$text);
            
    $text str_replace("=(""<img src='images/smileys/"$smileys[4] .".gif' />"$text);
            
    $text str_replace(":-(""<img src='images/smileys/"$smileys[4] .".gif' />"$text);
            
            
    $text str_replace(":'(""<img src='images/smileys/"$smileys[5] .".gif' />"$text);
            
    $text str_replace("='(""<img src='images/smileys/"$smileys[5] .".gif' />"$text);
            
    $text str_replace(":'-(""<img src='images/smileys/"$smileys[5] .".gif' />"$text);
            
    $text str_replace(":cry:""<img src='images/smileys/"$smileys[5] .".gif' />"$text);
            
            
    $text str_replace(":|""<img src='images/smileys/"$smileys[6] .".gif' />"$text);
            
    $text str_replace(":-|""<img src='images/smileys/"$smileys[6] .".gif' />"$text);
            
            
    $text str_replace(":@""<img src='images/smileys/"$smileys[7] .".gif' />"$text);
            
    $text str_replace(":-@""<img src='images/smileys/"$smileys[7] .".gif' />"$text);
            
            
    $text str_replace("(l)""<img src='images/smileys/"$smileys[8] .".gif' />"$text);
            
    $text str_replace("(L)""<img src='images/smileys/"$smileys[8] .".gif' />"$text);
            
            
    $text str_replace(":police:""<img src='images/smileys/"$smileys[9] .".gif' />"$text);
            
    $text str_replace(":cop:""<img src='images/smileys/"$smileys[9] .".gif' />"$text); 
    I've tried something with more arrays but I got confused :P
    Any suggestions ?


  2. #2
    Fuck you, I'm a dragon Pieman is offline
    Grand MasterRank
    Apr 2005 Join Date
    The NetherlandsLocation
    7,412Posts

    Re: [PHP]A way of shortening this huuuge code :P

    Holy fuck.

    First of all, only use 1 command for a smiley instead of 7. Second of all, use preg_replace.

    PHP Code:

    function replace_smiley($str) {

    $spattern = array(
    "/:\)/",
    "/:\(/",
    "/:(P|p)/"
    //etc...
    );

    $replace = array(
    "smileycodehere",
    "smileycodehere",
    "smileycodehere"
    );

    $str preg_replace($spattern$replace$str);

    return 
    $str;

    Normally I'd use indentation but I can't use the tab key in this text editor.

    Read this if you have trouble with the regular expressions used in the search pattern.

  3. #3
    Sorcerer Supreme admLoki is offline
    Member +Rank
    Apr 2005 Join Date
    www.codenetwork.ruLocation
    345Posts

    Re: [PHP]A way of shortening this huuuge code :P

    Holy Molly...
    That part of code is rather slow, isn't it?

    You can use preg_replace as Pieman said, or you can use str_replace with arrays as variables. And i doesn't see any reasons to use preg_replace when you haven't a regexp-patterns :
    PHP Code:
    function replaceSmiley($string)
    {
    $smilies = array(':-)',':-(',':-D');
    $replacement = array('codehere','codehere','codehere');
    return 
    str_replace($smilies,$replacement,$string);

    Last edited by admLoki; 30-08-08 at 07:06 PM.

  4. #4
    The Gamma..? EliteGM is offline
    Grand MasterRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    Re: [PHP]A way of shortening this huuuge code :P

    @pie:
    instead of doing "/:(P|p)/" can't you do "/:p/i" ?

  5. #5
    Sorcerer Supreme admLoki is offline
    Member +Rank
    Apr 2005 Join Date
    www.codenetwork.ruLocation
    345Posts

    Re: [PHP]A way of shortening this huuuge code :P

    Quote Originally Posted by EliteGM View Post
    instead of doing "/:(P|p)/" can't you do "/:p/i" ?
    Instead of "/:(P|p)/" required to use "/:p/i".

  6. #6
    Fuck you, I'm a dragon Pieman is offline
    Grand MasterRank
    Apr 2005 Join Date
    The NetherlandsLocation
    7,412Posts

    Re: [PHP]A way of shortening this huuuge code :P

    Oh dear, that was a stupid error. :x

  7. #7
    The Gamma..? EliteGM is offline
    Grand MasterRank
    Jul 2006 Join Date
    NandolandLocation
    4,077Posts

    Re: [PHP]A way of shortening this huuuge code :P

    Ty guys, fixed it up ;P



Advertisement