note 44209 modified in function.preg-match by tularis
| From: | tularis@php.net | Date: | Sun, 18 Jul 2004 11:28:20 +0000 |
| Subject: | note 44209 modified in function.preg-match by tularis | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-73474@lists.php.net to get a copy of this message | ||
[Editor's note: When adding the i Pattern Modifier, you can also test on lowercase words. This
means you won't need to have everything in sentence case/upper case at start]
Testing for indefinite articles in the English Language.
As I'm new to PHP and Regex, I decided to practice on something that would make my pages more
sophisticated.
When choosing from a drop-down menu of models of cars (e.g. Civic, CR-V, Accord etc.) I wanted to
then be able to output: "Joe drives a Civic." or "Mary drives an Accord."
The difference is the way "a" & "an" are used in English.
This simple group of functions helped me grab the first letter, test if it was a vowel and then
choose the proper article.
<?php
$first_letter = substr($value, 0, 1);
// $value is the string you're grabbing the first letter from
if(preg_match("/^A|E|I|O|U/", $first_letter))
{
$article = "an";
}
else
{
$article = "a";
}
?>
--was--
Testing for indefinite articles in the English Language.
As I'm new to PHP and Regex, I decided to practice on something that would make my pages more
sophisticated.
When choosing from a drop-down menu of models of cars (e.g. Civic, CR-V, Accord etc.) I wanted to
then be able to output: "Joe drives a Civic." or "Mary drives an Accord."
The difference is the way "a" & "an" are used in English.
This simple group of functions helped me grab the first letter, test if it was a vowel and then
choose the proper article.
<?php
$first_letter = substr($value, 0, 1);
// $value is the string you're grabbing the first letter from
if(preg_match("/^A|E|I|O|U/", $first_letter))
{
$article = "an";
}
else
{
$article = "a";
}
?>
http://php.net/manual/en/function.preg-match.php