dsaba Posted April 9, 2007 Share Posted April 9, 2007 hello everyone! i'm having a problem with a loop 1. it appears to be endless, as it freezes up my browser and never really ends 2. the variables are empty in the loop period <?php #scenario 2, 1 file with english=translation $fileArray = file('es-translations.txt') or die("cannot make array out of file lines"); //print_r($fileArray); for ($i=0; $i=count($fileArray); $i++) { echo '<br>'.$fileArray[$i]; $full_line = explode("=", $fileArray[$i]); $english = str_replace("", "_", trim($full_line[0])); $translation = trim($full_line[1]); $string = 'define('.$english.', "'.$translation.'", TRUE);'; echo '<br>'.$string; } ?> the end output should be: define(some_words_like_this, "some other words like this", TRUE); what it outputs is: define(, "", TRUE); i print_r($fileArray) and it did indeed have values so two problems I see: 1. endless loop 2. empty variables Any ideas? -thanks Quote Link to comment https://forums.phpfreaks.com/topic/46325-solved-endless-loop-empty-variables-what-is-going-on/ Share on other sites More sharing options...
wildteen88 Posted April 9, 2007 Share Posted April 9, 2007 Could you post some demo data stored in es-translations.txt Also this line is weird: $english = str_replace("", "_", trim($full_line[0])); Your are replacing nothing with an underscore - You might want to place the first two parameters for the str_replace function the other way around. That way you replace underscores with nothing. You cant replace nothing with something. Quote Link to comment https://forums.phpfreaks.com/topic/46325-solved-endless-loop-empty-variables-what-is-going-on/#findComment-225370 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2007 Share Posted April 9, 2007 In this line <?php $english = str_replace("", "_", trim($full_line[0])); ?> Are you sure you want to replace the null string with "_"? Maybe you want to replace all " " (spaces characters) with "_". Ken Quote Link to comment https://forums.phpfreaks.com/topic/46325-solved-endless-loop-empty-variables-what-is-going-on/#findComment-225374 Share on other sites More sharing options...
dsaba Posted April 9, 2007 Author Share Posted April 9, 2007 all the lines look like this in es-translations.txt optional not disclosed= opcional no cerrado OR optional not disclosed=opcional no cerrado i am replacing the english phrases to: optional_not_disclosed so it will look like: define(optional_not_disclosed, "opcional no cerrado", TRUE); and someone told me I need to make the for statement like this: for ($i=0; $i<count($fileArray); $i++) { so i'm going to try that out now yeah that works i wasn't using the FOR statement correctly and i will do str_replace " ", with "_" as well that should fix it thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/46325-solved-endless-loop-empty-variables-what-is-going-on/#findComment-225376 Share on other sites More sharing options...
kenrbnsn Posted April 9, 2007 Share Posted April 9, 2007 Here's a shorter version of your code: <?php $fileArray = file('es-translations.txt') or die("cannot make array out of file lines"); //print_r($fileArray); $tmp = array(); foreach ($fileArray as $val) { list($english, $translation) = explode("=", $val); $tmp[] = 'define('. trim(str_replace(' ','_',$english)) .', "'. trim($translation) .'", TRUE);'; } echo implode('<br>',$tmp).'<br>'; ?> Ken Quote Link to comment https://forums.phpfreaks.com/topic/46325-solved-endless-loop-empty-variables-what-is-going-on/#findComment-225382 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.