Page 1 of 1
Only words with vowels
Posted: Mon Oct 31, 2005 7:06 pm
by Extremest
I am trying to figure out a way to have php check a word to see if it contains a vowel if it does I can use it if not then skip it pretty much. Like an if statement. Can someone please help.
Posted: Mon Oct 31, 2005 7:19 pm
by feyd
regex can perform the function..
preg_match_all() using the \b metacharacter and a character class including all the vowels... although a huge amount of the words will contain a vowel, so I'm somewhat failing to see why you need to find all words with vowels..

Posted: Mon Oct 31, 2005 7:32 pm
by Extremest
I have a script that will go through html java and everything else removing the special chars and numbers. and replace with a space. Then anything under 5 characters is removed. Somehow I end up with some that are like jkjkdfghgrt that. I would like to have it remove these also as they are not words. I forgot to mention that each time it checks there will be only one word for it to check at a time.
Posted: Tue Nov 01, 2005 2:41 am
by Jenk
I'm by no means a regex guru, but this may be a starting point:
Code: Select all
<?php
$string = "tyty oh asdasdasd hghgjthrhfhg aeiuo gbgbghnghnghngbgbgb";
$string2 = preg_replace('/(^|\s)+\b[^aeiou]+?\b/i', '', $string);
echo trim($string) . "<br />\n";
echo trim($string2) . "<br />\n";
?>
Outputs:
Code: Select all
tyty oh asdasdasd hghgjthrhfhg aeiuo gbgbghnghnghngbgbgb<br />
oh asdasdasd aeiuo<br />
Posted: Tue Nov 01, 2005 12:54 pm
by pickle
strpos() will be your best bet. It's faster than regular expressions. This should work:
Code: Select all
$position_of_vowel = strpos($word_to_check,array('a','e','i','o','u'));
Posted: Tue Nov 01, 2005 1:05 pm
by Chris Corbyn
If you use strpos remember that a word such as "alfie" or "orange" will return a zero and so will fail a truth test unless you use the === (identical to) operator.
Code: Select all
if (strpos($word, array('a', 'e', 'i', 'o', 'u')) !== false)
{
// <ding!>
}
/* rather than */
if (strpos($word, array('a', 'e', 'i', 'o', 'u')))
{
// <dong!>
}