preg_match - Can't get it - how it works

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
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

preg_match - Can't get it - how it works

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

Post 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);

?>
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

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

Post by feyd »

$text can and was assumed to be the entirity of the text you want to check for vowel ending words.
User avatar
Calimero
Forum Contributor
Posts: 310
Joined: Thu Jan 22, 2004 6:54 pm
Location: Milky Way

Post by Calimero »

Muchas Gracias Senor,

For your help :)
Post Reply