The right function

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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("(wink)", "<IMG SRC=\"wink.gif\">", $mytext);
$mytext= str_replace("(smile)", "<IMG SRC=\"smile.gif\">", $mytext);
$mytext= str_replace("(laugh)", "<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?


Re: The right function

Quoted text here. Click to load it

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.

Re: The right function

Quoted text here. Click to load it

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

See example 1 on (specifically the
pizza/beer/... lines).

So it just appears that you should RTFM :)


  Daniel Tryba

Re: The right function

On Tue, 17 Aug 2004 22:56:16 +0100, "Paul C-T"

Quoted text here. Click to load it
 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.

< Space: disk usage analysis tool

Re: The right function


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 !!! ;-)


Quoted text here. Click to load it
Quoted text here. Click to load it
set up

Site Timeline