Jump to content

Recommended Posts

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

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.