Replacing every character in a string with individual <img> tags

Discussion in 'Perl Misc' started by PaddyPerl@gmail.com, Sep 10, 2007.

  1. Guest

    Hi everybody!

    OK, so what I want to do is to display a string in html as images
    rather than text. On the server I have individual image files a.gif,
    b.gif, c.gif ... 0.gif, 1.gif, 2.gif, 3.gif etc...
    So if the string is Hello, I would like to get the following output:
    <img src="H.gif"><img src="e"><img src="l"><img src="l"><img src="o">

    I would like to translate all characters, numbers and special
    characters as well.

    Shouldn't this be quite easy? I just don't know how to do it though...
    Thanks a lot for your help!

    /Paddy
     
    , Sep 10, 2007
    #1
    1. Advertising

  2. Paul Lalli Guest

    On Sep 10, 6:05 pm, wrote:
    > Hi everybody!
    >
    > OK, so what I want to do is to display a string in html as images
    > rather than text. On the server I have individual image files a.gif,
    > b.gif, c.gif ... 0.gif, 1.gif, 2.gif, 3.gif etc...
    > So if the string is Hello, I would like to get the following output:
    > <img src="H.gif"><img src="e"><img src="l"><img src="l"><img src="o">
    >
    > I would like to translate all characters, numbers and special
    > characters as well.
    >
    > Shouldn't this be quite easy? I just don't know how to do it
    > though...


    In general, you should always post your best attempt when asking for
    help.

    However, I'm bored.

    $string = s/(.)/<img src="$1.gif">/g;

    Paul Lalli
     
    Paul Lalli, Sep 10, 2007
    #2
    1. Advertising

  3. Guest

    On Sep 10, 3:10 pm, Paul Lalli <> wrote:
    > $string = s/(.)/<img src="$1.gif">/g;


    Surely Paul meant
    $string =~ s/(.)/<img src="$1.gif">/g;

    But you need to consider if double-quote is one of the "special
    characters" that you want to allow...



    --
    David Filmer (http://DavidFilmer.com)
     
    , Sep 11, 2007
    #3
  4. Paul Lalli Guest

    On Sep 10, 7:47 pm, wrote:
    > On Sep 10, 3:10 pm, Paul Lalli <> wrote:
    >
    > > $string = s/(.)/<img src="$1.gif">/g;

    >
    > Surely Paul meant
    > $string =~ s/(.)/<img src="$1.gif">/g;


    Took me five reads to realize you're pointing out the fact that I used
    = where I meant =~

    Thanks,
    Paul Lalli
     
    Paul Lalli, Sep 11, 2007
    #4
  5. wrote:
    >
    > $string =~ s/(.)/<img src="$1.gif">/g;
    >
    > But you need to consider if double-quote is one of the "special
    > characters" that you want to allow...


    Special? In what sense?

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 11, 2007
    #5
  6. Guest

    > $string =~ s/(.)/<img src="$1.gif">/g;
    >


    Great! Thanks alot to all of you!
    But... I've now realized I have another problem...
    With special characters such as Ä, I decided to go for filenames such
    as AE.gif to avoid problems with encoding, and first tried a
    $string =~ s/Ä/AE/g;
    prior to the above, but then of course I'm left with two images (A and
    E) instead of the desired AE.gif. Is there a way to avoid a lot of if
    statements here?

    Paddy
     
    , Sep 11, 2007
    #6
  7. wrote:
    >> $string =~ s/(.)/<img src="$1.gif">/g;

    >
    > Great! Thanks alot to all of you!
    > But... I've now realized I have another problem...
    > With special characters such as Ä, I decided to go for filenames such
    > as AE.gif to avoid problems with encoding, and first tried a
    > $string =~ s/Ä/AE/g;
    > prior to the above, but then of course I'm left with two images (A and
    > E) instead of the desired AE.gif. Is there a way to avoid a lot of if
    > statements here?


    Use a hash.

    my %special = (
    'Ä' => 'AE',
    'Ö' => 'OE',
    );

    s/(.)/'<img src="'.($special{$1} or $1).'.gif">'/eg;

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
     
    Gunnar Hjalmarsson, Sep 11, 2007
    #7
  8. Gunnar Hjalmarsson <> writes:

    > wrote:
    >>
    >> $string =~ s/(.)/<img src="$1.gif">/g;
    >>
    >> But you need to consider if double-quote is one of the "special
    >> characters" that you want to allow...

    >
    > Special? In what sense?


    In the senses that <img src="".gif"> probaly isn't parsed the intended
    way by most HTML readers.

    //Makholm
     
    Peter Makholm, Sep 11, 2007
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. news.austin.rr.com

    how to download img from html img tag

    news.austin.rr.com, Apr 27, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    540
    Steve C. Orr [MVP, MCSD]
    Apr 27, 2005
  2. Philipp Lenssen

    Problem: <img></img>

    Philipp Lenssen, Jun 27, 2005, in forum: XML
    Replies:
    15
    Views:
    805
    David Håsäther
    Jul 3, 2005
  3. Jopek
    Replies:
    7
    Views:
    428
    dorayme
    Apr 26, 2009
  4. Immortal Nephi
    Replies:
    3
    Views:
    810
    James Kanze
    Jun 10, 2009
  5. replacing tags between tags

    , Sep 18, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    141
    Jürgen Exner
    Sep 19, 2005
Loading...

Share This Page