PDA

View Full Version : Initialism


Schannah
04-14-2006, 05:12 PM
I know this may seem a bit of an obscure question, but does anybody know of a way in which I can automatically isolate the initial of a word using HTML and/or CSS?

If it helps at all, the context is that the word will be a variable name, i.e. the username of the person who most recently visited a certain page (e.g. John). I want to find a way of coding it such that I can show just the initial of the word (e.g. J). If this is a viable solution I can code it such that the word is all in lowercase other than the initial letter and then isolate and use the only capital letter in the word, but then obviously I would need a method to isolate the capital.

I'm doing a Gorey-based layout and as an end result, I want to be able to have a section that goes something like:

"J is for John smothered under a rug"
where John is the variable username, and J is the automatic initial. In the layout I will only be able to input the variable of the name itself so I need to code the rest.

I probably haven't explained this very well. Anybody who has any idea what I'm talking about, though: please help!

-- Hannah

kewlceo
04-14-2006, 05:48 PM
You could insert a script into your HTML. I assume either PHP or JavaScript would be employed anyway, so here's some code snippets in both:

If you had the name in a PHP string called $firstName, then you could use the following bit of code to copy the first initial into a string named $initial:

<?
$firstName = "John";
$initial = $firstName{0};
echo $initial;
?>

In JavaScript:
<script language="JavaScript">
<!--
var firstName = prompt("Please enter your first name:", "");
if (firstName) {
var initial = (firstName.substring(0,1));
document.write("<h3>Hi "+firstName+"! Your initial is "+initial+".</h3>");
} else
document.write("<h3>John Doe, I presume?</h3>");
// -->
</script>

Schannah
04-15-2006, 12:51 AM
Truly, truly awesome. Thank you. I don't know any javascript but with a little common sense I managed to play around with your code there to make it work in the context I need. Thank you so much.

kewlceo
04-15-2006, 01:05 AM
That's cool, Hannah, I'm glad you were able to adapt it! :)