Jump to content

[SOLVED] question about finding the first letter of a word


Renlok

Recommended Posts

Assuming that you are dealing with words you know always begin with a letter (not quotes or any other character), you could simply do one of the following:

<?php
$word = "Hello";
$letter = $word{0};
$letter = substr(0, 1, $word);
?>

 

Now, if you want to make sure that the first letter is pulled, even if your string contains another starting character, you'll have to account for that with some sort of pattern match:

<?php
$word = "'ello";
preg_match('|[^a-z]+?([a-z])|i', $word, $match);
$letter = $match[1];
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.