Delphi For PHP Forums    


Go Back   Delphi-PHP Forums > Programming > PHP - Code
Forum Jump Register FAQ Members List Downloads Search Today's Posts Mark Forums Read

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.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 8th June 2009, 16:04
just Joined!
 
Join Date: Jun 2009
Posts: 4
Duke555 is on a distinguished road
Exclamation ""/'' or regex bug

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;
}
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT +1. The time now is 04:58.




Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0 ©2009, Crawlability, Inc.
Copyright © 2004 - 2009, G&J Solutions Ltd. All Rights Reserved. terms of use