Renlok Posted April 10, 2007 Share Posted April 10, 2007 Is it possible to find the first letter of a word? Or to find what the first letter is? If there is a way what is it? i cant find out how anywhere. thanks in advance for any help. Link to comment https://forums.phpfreaks.com/topic/46455-solved-question-about-finding-the-first-letter-of-a-word/ Share on other sites More sharing options...
kenrbnsn Posted April 10, 2007 Share Posted April 10, 2007 There are a number of ways. Here are two: <?php $word = 'this'; $fl1 = $word[0]; $fl2 = substr(0,1,$word); ?> Ken Link to comment https://forums.phpfreaks.com/topic/46455-solved-question-about-finding-the-first-letter-of-a-word/#findComment-225981 Share on other sites More sharing options...
obsidian Posted April 10, 2007 Share Posted April 10, 2007 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]; ?> Link to comment https://forums.phpfreaks.com/topic/46455-solved-question-about-finding-the-first-letter-of-a-word/#findComment-225985 Share on other sites More sharing options...
Renlok Posted April 10, 2007 Author Share Posted April 10, 2007 well they always begin with a letter well cheers for that. Link to comment https://forums.phpfreaks.com/topic/46455-solved-question-about-finding-the-first-letter-of-a-word/#findComment-225996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.