The right function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Paul C-T

    #1

    The right function

    Hi,

    Don't know the right function to use here. I have a php file containing
    text only that will contain references to smilies formatted such as "...
    then I told him the punch line (wink) ..." - obviously I want to detect the
    (xxx) smilie and replace with an image.

    I can sort of do this using multiple lines of code like this ...

    $mytext= str_replace("(w ink)", "<IMG SRC=\"wink.gif\ ">", $mytext);
    $mytext= str_replace("(s mile)", "<IMG SRC=\"smile.gif \">", $mytext);
    $mytext= str_replace("(l augh)", "<IMG SRC=\"laugh.gif \">", $mytext);

    .... but I would prefer to find a function that could detect the (xxx) and
    simply call the relevant image using a variable ... <IMG SRC="$image.gif ">

    Any ideas?

    Paul.


  • Andy Hassall

    #2
    Re: The right function

    On Tue, 17 Aug 2004 22:56:16 +0100, "Paul C-T"
    <paulcharltonth omson@hotmail.c om> wrote:
    [color=blue]
    >Don't know the right function to use here. I have a php file containing
    >text only that will contain references to smilies formatted such as "...
    >then I told him the punch line (wink) ..." - obviously I want to detect the
    >(xxx) smilie and replace with an image.
    >
    >I can sort of do this using multiple lines of code like this ...
    >
    >$mytext= str_replace("(w ink)", "<IMG SRC=\"wink.gif\ ">", $mytext);
    >$mytext= str_replace("(s mile)", "<IMG SRC=\"smile.gif \">", $mytext);
    >$mytext= str_replace("(l augh)", "<IMG SRC=\"laugh.gif \">", $mytext);
    >
    >... but I would prefer to find a function that could detect the (xxx) and
    >simply call the relevant image using a variable ... <IMG SRC="$image.gif ">
    >
    >Any ideas?[/color]

    Are you sure you want to make it unrestricted? You've only got a finite number
    of images, surely, so you'd want to just limit it to that. In which case set up
    an array, and loop over the array doing str_replaces as above.

    $images = array('wink', 'smile', 'laugh');
    foreach ($images as $image)
    $mytext= str_replace("($ image)", "<IMG SRC='$image.gif '>", $mytext);

    If you do want it unrestricted, then preg_replace is probably the direction
    you want to go.

    --
    Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
    <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool

    Comment

    • Paul C-T

      #3
      Re: The right function

      Andy,

      Thank you ... I am soooo stupid ... well actually it's late ... I knackered
      .... and I promised myself I would do coding when I'm knackered !!! ;-)

      Paul.


      "Andy Hassall" <andy@andyh.co. uk> wrote in message
      news:8315i0dl5r im91qcqa0c3cbsj vf2mqtvnf@4ax.c om...[color=blue]
      > On Tue, 17 Aug 2004 22:56:16 +0100, "Paul C-T"
      > <paulcharltonth omson@hotmail.c om> wrote:
      >[color=green]
      > >Don't know the right function to use here. I have a php file containing
      > >text only that will contain references to smilies formatted such as "...
      > >then I told him the punch line (wink) ..." - obviously I want to detect[/color][/color]
      the[color=blue][color=green]
      > >(xxx) smilie and replace with an image.
      > >
      > >I can sort of do this using multiple lines of code like this ...
      > >
      > >$mytext= str_replace("(w ink)", "<IMG SRC=\"wink.gif\ ">", $mytext);
      > >$mytext= str_replace("(s mile)", "<IMG SRC=\"smile.gif \">", $mytext);
      > >$mytext= str_replace("(l augh)", "<IMG SRC=\"laugh.gif \">", $mytext);
      > >
      > >... but I would prefer to find a function that could detect the (xxx) and
      > >simply call the relevant image using a variable ... <IMG[/color][/color]
      SRC="$image.gif ">[color=blue][color=green]
      > >
      > >Any ideas?[/color]
      >
      > Are you sure you want to make it unrestricted? You've only got a finite[/color]
      number[color=blue]
      > of images, surely, so you'd want to just limit it to that. In which case[/color]
      set up[color=blue]
      > an array, and loop over the array doing str_replaces as above.
      >
      > $images = array('wink', 'smile', 'laugh');
      > foreach ($images as $image)
      > $mytext= str_replace("($ image)", "<IMG SRC='$image.gif '>", $mytext);
      >
      > If you do want it unrestricted, then preg_replace is probably the[/color]
      direction[color=blue]
      > you want to go.
      >
      > --
      > Andy Hassall / <andy@andyh.co. uk> / <http://www.andyh.co.uk >
      > <http://www.andyhsoftwa re.co.uk/space> Space: disk usage analysis tool[/color]


      Comment

      • Daniel Tryba

        #4
        Re: The right function

        Paul C-T <paulcharltonth omson@hotmail.c om> wrote:[color=blue]
        > I can sort of do this using multiple lines of code like this ...
        >
        > $mytext= str_replace("(w ink)", "<IMG SRC=\"wink.gif\ ">", $mytext);
        > $mytext= str_replace("(s mile)", "<IMG SRC=\"smile.gif \">", $mytext);
        > $mytext= str_replace("(l augh)", "<IMG SRC=\"laugh.gif \">", $mytext);[/color]

        str_replace accepts arrays for the first and second parameter...

        See example 1 on http://php.net/str_replace (specifically the
        pizza/beer/... lines).

        So it just appears that you should RTFM :)

        --

        Daniel Tryba

        Comment

        • Chung Leong

          #5
          Re: The right function

          "Paul C-T" <paulcharltonth omson@hotmail.c om> wrote in message
          news:41227f0a$0 $91739$ed2e19e4 @ptn-nntp-reader04.plus.n et...[color=blue]
          > Hi,
          >
          > Don't know the right function to use here. I have a php file containing
          > text only that will contain references to smilies formatted such as "...
          > then I told him the punch line (wink) ..." - obviously I want to detect[/color]
          the[color=blue]
          > (xxx) smilie and replace with an image.
          >
          > I can sort of do this using multiple lines of code like this ...
          >
          > $mytext= str_replace("(w ink)", "<IMG SRC=\"wink.gif\ ">", $mytext);
          > $mytext= str_replace("(s mile)", "<IMG SRC=\"smile.gif \">", $mytext);
          > $mytext= str_replace("(l augh)", "<IMG SRC=\"laugh.gif \">", $mytext);
          >
          > ... but I would prefer to find a function that could detect the (xxx) and
          > simply call the relevant image using a variable ... <IMG SRC="$image.gif ">
          >
          > Any ideas?[/color]

          Use glob() to scan for these images, then construct a replacement array for
          strtr() based on the filenames. That way you can add additional smilies by
          simply uploading new images.


          Comment

          Working...