Just something for making emoticons in php

$smiley = htmlspecialchars('I make smiles :) ');
$icon= array(':)', ':D');
$replacements = array(
				 '<img src="smile.jpg"/>', ' <img src="grin.jpg"/>'
				);
$smiley = str_replace($icon, $replacements, $smiley);

echo $smiley;

    I might do it something like this:

    
    function parseEmoticons($text)
    {
        $emoticons = array(
            '/(?<=^|\s|\b):\)(?=\s|\b|$)/' => '<img src="smile.png">',
            '/(?<=^|\s|\b):D(?=\s|\b|$)/' => '<img src="grin.png">'
        );
        return preg_replace(array_keys($emoticons), $emoticons, $text);
    }
    
    $text = "Grin: :D, (but not :Dthis:), smile: :)";
    echo parseEmoticons($text).PHP_EOL;
    

    Output:

    [~]$ php smilies.php
    Grin: <img src="grin.png">, (but not :Dthis:), smile: <img src="smile.png">
    

      Or, I might re-factor it as this, to make it a bit DRY-er, and also easier to maintain when you want to add more emoticons:

      <?php
      
      function parseEmoticons($text)
      {
          $emoticons = array(
              ':)' => '<img src="smile.png">',
              ':D' => '<img src="grin.png">'
          );
          $left   = '/(?<=^|\s|\b)';
          $right  = '(?=\s|\b|$)/';
          $search = array();
          foreach($emoticons as $key => $value) {
              $search[$left.preg_quote($key).$right] = $value;
          }
          return preg_replace(array_keys($search), $search, $text);
      }
      
      $text = "Grin: :D, (but not :Dthis:), smile: :)";
      echo parseEmoticons($text).PHP_EOL;
      

      🙂

        We both know I will 😉, guess if should use functions more often, my problem with functions is they seem to lack specificity if that makes sense

          There are two complementary reasons to use functions. One is for anything you are going to use multiple times and/or in multiple applications, so that you can just define the function once and then call it as needed -- that's the Don't Repeat Yourself reason. The second reason is simply to help with code readability, and therefore code maintenance. If you move bits of fiddly stuff into dedicated functions, you don't need all those lines of fiddly stuff in the main flow of your application, making it easier to see what that main flow is. If the functions are well named, then it's (hopefully) pretty obvious what each call is doing, so the maintainer can make a good guess if s/he needs to dive into it, or can ignore it for the current issue being looked at.

          Which then starts to get you a step or two away from object-oriented PHP, using classes and their properties and methods to group related "things" together into classes, which the main code uses as needed. Maybe you'd have a ForumPost class, which might include the parseEmoticons() method among its several different methods for working with forum posts -- or it might make more sense in a more general ForumHTML "helper" class, which the ForumClass would instantiate and use if needed. Or.... 🙂

            Makes more sense, I have been working my way through objects, patterns and practices 4rth edition which has been kinda fun, one day I'll look back at my current index page and shudder I'm sure lol

              Heh...pretty much anything you write looks stupid a year or so later. 😉

                NogDog;11057489 wrote:

                Heh...pretty much anything you write looks stupid a year or so later. 😉

                I prefer the term 'dated' ... 😉

                  NogDog;11057489 wrote:

                  Heh...pretty much anything you write looks stupid a year or so later. 😉

                  A year? You're like a god! I come back a week later and go "Wtf were you thinking Ryan??"

                    Derokorian;11057497 wrote:

                    A year? You're like a god! I come back a week later and go "Wtf were you thinking Ryan??"

                    That's soon enough that you can just claim agile process: get a minimum viable product, and then start re-factoring. 🙂

                      lol a year? A week? Sometimes I come back the next day and sigh and my ramblings from the previous :rolleyes:

                        NogDog;11057499 wrote:

                        That's soon enough that you can just claim agile process: get a minimum viable product, and then start re-factoring. 🙂

                        Refactoring? Luxury. Who can refactor when they're supposed to be on to the next project by then?

                          Weedpacket;11057507 wrote:

                          Refactoring? Luxury. Who can refactor when they're supposed to be on to the next project by then?

                          Agreed. First testable version is the shipped version....

                            Write a Reply...