Forum Moderators: coopster

Message Too Old, No Replies

Breaking up text in variable and putting each character into a tag

         

Jeremy_H

1:54 am on Jul 7, 2006 (gmt 0)

10+ Year Member



Hello,

I have a PHP variable called $tname.

Lets say $tname="Norma";

Right now I have it setup to output this:

<p><?php echo($tname);?></p>

Which turns into:

<p>Norma</p>

But what I would really like to output is this:

<p><img src="/font/caps/n.gif"><img src="/font/o.gif"><img src="/font/r.gif"><img src="/font/m.gif"><img src="/font/a.gif"></p>

To confound this issue even more, $tname can be any length and the first character is not always capitalized, nor is it the only one that can be capitalized.

Any ideas how I can go about dong this?

Thanks

eelixduppy

2:11 am on Jul 7, 2006 (gmt 0)



How about this?:

//strip characters you do not want here
$length = strlen [us3.php.net]($tname);
echo '<p>';
for($i = 0; $i < $length; $i++) {
if(ctype_upper [us3.php.net]($tname[$i]) { echo '<img src="/font/caps/'.strtolower [us3.php.net]($tname[$i]).'.gif" />'; }
else { echo '<img src="/font/'.$tname[$i].'.gif" />'; }
}
echo '</p>';

Something like this (not tested). I wouldn't suggest using a large amount of font for this. Good luck!

alce

2:38 am on Jul 7, 2006 (gmt 0)

10+ Year Member



Here's a slightly different example that might also work, depending on what you need:

<?php

$tname = "Norma";
$varToLower = strtolower($tname);
$length = strlen($varToLower);

echo "<p>";
for( $i = 0; $i < $length; $i++) {
echo "<img src=\"/font/caps/$varToLower[$i].gif\" alt=\"$varToLower[$i]\">\n";
}
echo "</p>";

?>

Nutter

4:21 am on Jul 7, 2006 (gmt 0)

10+ Year Member



Couldn't you use the GD functions to make one graphic file of Norma rather than using 5 separate files? I've never used GD, so I have no clue how to go about it.

You might look at page counter scripts. Seems like a pretty similar function, except counters only have 10 characters to deal with and you have 62.

Jeremy_H

4:39 pm on Jul 7, 2006 (gmt 0)

10+ Year Member



Thanks so much for the help with the script, it works great.

Nutter, I'll have to take a look into that. I know that using five+ separate images will definitely cause my sight to slow down on this one page, but I thought having a image generator would use a lot of system overhead. I'll have to look into that.

Thanks.