note 44209 added to function.preg-match
| From: | fungineer at gmail dot com | Date: | Sun, 18 Jul 2004 07:50:30 +0000 |
| Subject: | note 44209 added to function.preg-match | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-73471@lists.php.net to get a copy of this message | ||
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";
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.preg-match.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+44209
Delete -- http://master.php.net/manage/user-notes.php?action=delete+44209&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+44209&report=yes
Search -- http://master.php.net/manage/user-notes.php