treehouse : what would you like to learn today?
Web Design Web Development iOS Development

How to add a DIV to this JS code

  • I have a comment embedder plugin that allow my users to upload a photo with their comments. Now I want to give that photo, a special DIV. Here is its main code:

    function embed_images($content) {
    $content = preg_replace('/\[img=?\]*(.*?)(\[\/img)?\]/e', '"<img src=\"$1\" alt=\"" . basename("$1") . "\" />"', $content);
    return $content;
    

    }

    Where to add the DIV? *the image add with a [img][/img] shortcode to the comment.

  • There's a few easy ways to handle this. You could wrap your $content with a string "

    " + preg.... + "

    "; Or more simply add another line after $content.

      function embed_images($content) {
          $content = preg_replace('/\[img=?\]*(.*?)(\[\/img)?\]/e', '"<img src=\"$1\" alt=\"" . basename("$1") . "\" />"', $content);
          $content = "<div>" + $content + "</div>";
          return $content;
      }
    

    The second options would be simpler to read. Also this is just a php way of doing it, If you wanted to you could have a js library handle the images and wrap them as well.

  • Oh wow markdown stipped the first examle.

      $content = "<div>" + preg_replace('/\[img=?\]*(.*?)(\[\/img)?\]/e', '"<img src=\"$1\" alt=\"" . basename("$1") . "\" />"', $content) + "</div">;