![]() |
|
|
|||||||
| PHP - Code PHP programming - Ask questions and help people with PHP code. If you are stuck and need help, this is where you ask for help. |
![]() |
|
|
LinkBack | Thread Tools | Display Modes |
|
|||
|
i have a bug in my code (below) and i cannot work out how to get rid of it.
when i test the code (by uncommenting one of '$sentence = ...') every word that begins with a '$' disappears and 'ay' gets added to where the word is supposed to be. when i change "" to '' all the problems go away. could someone explain to me why this is the case and what is causing it? thank you very much ![]() ========================================= <?php //echo "Word provided by the user is: ".$_POST['word']."<br>"; $sentence = $_POST['word']; /*$sentence = "@#%^##Hello World ?##World ?earner earner earner,? learner $$$learner";*/ /* $sentence = "@#%^##Hello World $world ?##World ?earner earner earner,? world $world";*/ $sentence = strtolower($sentence); //echo "converted to lower case: ".$sentence."<br>"; $cleaned_sentence = clean_string($sentence); echo $cleaned_sentence."<br>"; $translated = translate($cleaned_sentence); echo "The translated word/sentence into Pig Latin is: ".$translated."<br>"; /************************************************** **************************/ function clean_string($sentence_to_be_cleaned) { $bad_characters = "[^a-zA-Z]"; $new_sentence = ereg_replace($bad_characters, ' ', $sentence_to_be_cleaned); return $new_sentence; } function translate($string_to_translate) { // separating a sentence passed into individual words to translate into Pig Latin // echo "string passed to translate() is: ".$string_to_translate."<br>"; $string = explode(' ', $string_to_translate); // this regex will be used to check if the first letter is vowel or not $reg_expression = '(a|e|i|o|u)'; // go through the string passed by INPUT word by WORD foreach($string as $key=>$word) { //if 1st letter of a word is a vowel if(check_regex($reg_expression, substr($word,0,1)) == TRUE) { // concatenate pig latin suffix and a given word to a total string which is going to be returned by the func. in the end $total_string = $total_string.$word.'\'yay '; } // if a character is a ' ' (space) which occurs when any of the @#$? were replaced clean_string() then get rid of it else if($word == ' ') { $word = ''; } // else - 1st letter is a consonant else { // find, replace the 1st consonant and then concatenate to a string which is going to be returned by // the function $consonant = substr($word,0,1); $temp_string = substr_replace($word, "", 0, 1); echo $temp_string."---- "; // concatenate pig latin suffix and a given word to a total string which is going to be returned by the func. in the end $total_string = $total_string.$temp_string.$consonant."ay"." "; } } return $total_string; } function check_regex($myregex, $mystring) { if (ereg($myregex, $mystring)) { return TRUE; } else { return FALSE; } } ?> |
| Sponsored Links |
|
|