Duke555 Posted June 8, 2009 Share Posted June 8, 2009 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; } } ?> Link to comment https://forums.phpfreaks.com/topic/161375-or-regex-bug/ Share on other sites More sharing options...
Ken2k7 Posted June 9, 2009 Share Posted June 9, 2009 What line(s) am I supposed to look at? Link to comment https://forums.phpfreaks.com/topic/161375-or-regex-bug/#findComment-852071 Share on other sites More sharing options...
xtopolis Posted June 9, 2009 Share Posted June 9, 2009 Uhh.. referring to the only thing that looks like a question... there is a difference between literal and quoted strings. http://us.php.net/language.types.string Long story short, values inside " " are interpreted, while values inside ' ' are taken literally. Link to comment https://forums.phpfreaks.com/topic/161375-or-regex-bug/#findComment-852074 Share on other sites More sharing options...
Duke555 Posted June 9, 2009 Author Share Posted June 9, 2009 below are the lines where the bug is likely to be: when uncommented: /*$sentence = "@#%^##Hello World ?##World ?earner earner earner,? learner $$$learner";*/ /* $sentence = "@#%^##Hello World $world ?##World ?earner earner earner,? world $world";*/ 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; } Link to comment https://forums.phpfreaks.com/topic/161375-or-regex-bug/#findComment-852371 Share on other sites More sharing options...
MadTechie Posted June 9, 2009 Share Posted June 9, 2009 thats because $ needs to be escaped $eg = "\$test"; or just used single quotes $eg = '$test'; Link to comment https://forums.phpfreaks.com/topic/161375-or-regex-bug/#findComment-852377 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.