Page 1 of 1

preg_match - Can't get it - how it works

Posted: Tue Sep 14, 2004 2:16 pm
by Calimero
I have this problem,

I need php to recognise if the word is ending with one of these letters, that are declared as an array

$vocals = array ('a', 'e', 'i', 'o', 'u');

And if it is, to ECHO "for ex. this word ends with a vocal.

I spent two days in figuring out what the code should be, but no success, and I looked several times in the manual - but still don't get it


If possible I would need the code, please.

Posted: Tue Sep 14, 2004 2:22 pm
by feyd
regular expressions can do this fairly easy..

Code: Select all

<?php

$vocals = array('a', 'e', 'i', 'o', 'u');

$num_matches = preg_match_all('#\b.*?ї' . implode('',$vocals) . ']\b#i',$text,$matches);

var_dump($num_matches, $matches);

?>

Posted: Tue Sep 14, 2004 2:28 pm
by Calimero
Ok, Thanks !!

Just need to see if I understand:

In this row
//$num_matches = preg_match_all('#\b.*?[' . implode('',$vocals) . ']\b#i',$text,$matches);

$text is the WORD I check against
$matches - are results, right

and the last row - prints out the result


Thanks !!!

Posted: Tue Sep 14, 2004 2:38 pm
by feyd
$text can and was assumed to be the entirity of the text you want to check for vowel ending words.

Posted: Tue Sep 14, 2004 2:43 pm
by Calimero
Muchas Gracias Senor,

For your help :)