PDA

View Full Version : Php check if words ryhme


africano
09-12-2008, 08:45 AM
Hi Im trying to write some code to check if the last 3 letters in a word are the same, if so they ryhme if they dont match then they dont ryhme.

I need too use functions and variables.

So far I can compare the lenghth of two different words but I cant seem to figure this excersise out.....

Basically take two words, compare them and check if they rhyme following that validation rule.

Any help appreciated!

lux
09-12-2008, 09:21 AM
The function you need to use is substr: php.net/substr

I have written a function that takes two args (two strings) and compares the last three characters and checks to see if they are the same.


$word_one = "line from a poem";
$word_two = "another line from a poem";


function ryhme_check($first_string, $second_string){

$one = substr($first_string, -3);
$two = substr($second_string, -3);

if($one === $two){
$ryhme_result = "The last three letters of the string match.";
}else{
$ryhme_result = "The last three letters of the string are different.";
}

return $ryhme_result;
}

$blah = ryhme_check($word_one, $word_two);

echo $blah;


hope this helps

africano
09-12-2008, 09:29 AM
Cheers, having a look at the code know(getting to grips with it!)

Thanks once again.:grin: