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

P

PaddyPerl

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
 
P

Paul Lalli

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
 
U

usenet

$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...
 
P

Paul Lalli

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
 
G

Gunnar Hjalmarsson

$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?
 
P

PaddyPerl

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
 
G

Gunnar Hjalmarsson

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;
 
P

Peter Makholm

Gunnar Hjalmarsson said:
Special? In what sense?

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

//Makholm
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top