I want to be able to check wheter an inputted string has any of the characters in $alphabet and if it does display the relevant image per relevant character. so if someone entered hello. it would display h.png, e.png, l.png, l.png and o.png. So far I have got it to recognise the users input and echo it out and search whether it has a particular letter and output it to the relevant image via this code:
<?php
$input = trim($_POST["textarea"]);
echo $input;
echo "<br />";
if(strcmp($input[0],'a')==0){
echo "<img src='egypt/$input.png'>";
}else{
echo "You did not write a";
}
?>
Which works perfectly. However I tried to input more code which would allow a whole string including spaces to be analysed against the whole alphabet $alphabet and match each character in the string to the right image with this code below: but it doesnt work
<?php
$input = trim($_POST["textarea"]);
echo $input;
echo "<br />";
$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
while (list(, $value) = each($alphabet) AND list(, $input) = each($value2)) {
if(strcmp($value2[0],$value)==0){
echo "<img src='egypt/$value2.png'>";
}else{
echo "You did not write a";
}
}
?>
Above is the neccessary code.
Update: I have worked out how to match the first letter of input against any in the alphabet but still struggling to work out how to map against a whole string with spaces.
<?php
$input = trim($_POST["textarea"]);
echo $input;
echo "<br />";
$alphabet = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
if(strcmp($input[0],$alphabet)==0){
echo "<img src='egypt/$input.png'>";
}else{
echo "Error";
}
?>
Code with reference to update for anyone who cares
Let me suggest you a different route.
This will replace every letter (a-z) with its img equivalent, so you will get this result: