Only words with vowels

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Only words with vowels

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.. :?
Extremest
Forum Commoner
Posts: 84
Joined: Mon Aug 29, 2005 12:39 pm

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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 />
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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'));
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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!>
}
Post Reply