Foreach will not loop through text


New member
Foreach will not loop through text.

Hello. I've got an odd problem that I cannot resolve - a simple foreach.. I've done this technique dozens of times in other scripts but this one will not work.

:- The main script, gets the value of $ac (the file name).
:- it sends the file-name to the function get_textprocess

:- the function, gets the contents of the file, & sends it back to the main script..

If I activate this line, it saves the contents of $out into a file. *I AM GETTING CONTENT*.
// save_logs_hour("V01_GETTEXTPROCESS",$out);

However, when I go back to the main script, I do the foreach and am NOT GETTING ANY CONTENT.
- if i activate this line in the main script, I AM getting the content..
// $log.="$textone\n";

QUERY: Why can't I do a foreach loop on this content? Even if I did get the content as ONE big text instead of line-by-line, I'll expect at least one line to appear in the foreach loop.


// $log.="$textone\n";

foreach($textone as $aba => $aca)
$log.=" TEXTONE '$aba' '$aca'\n";
$log.="CHANNEL '$channel'\n";


function get_textprocess($source)
foreach($text as $abq => $acq)
// save_logs_hour("V01_GETTEXTPROCESS",$out); << file is 400K in size, I don't need it saved - This just to check content.

EDIT! If I skip out the function, & just use $textone=file($ac); it DOES work. Why doesn't the function do as expected?
Last edited:
It seems like the issue might be related to how the content is processed and returned from the get_textprocess function.
  1. Check the Return Value: Ensure that the get_textprocess function is returning an array. You can check this by using var_dump or print_r to inspect the value of $out before it's returned.
  2. Line Breaks in Content: Check if there are any unusual characters or line breaks in the content of the file. Sometimes, unexpected characters can cause issues with array functions like foreach.
  3. Debugging Output: Add debugging output within the get_textprocess function to ensure that the content is being processed correctly.
Here's how you can modify your code for debugging:

phpCopy code

function get_textprocess($source)
$source = trim($source);
$out = "";
$text = file($source);
foreach ($text as $abq => $acq) {
$lonen = trim($acq);
$out .= "$lonen\n";

// Debugging output
echo "Content in get_textprocess function: <br>";
echo nl2br($out); // Output the content with line breaks preserved
// save_logs_hour("V01_GETTEXTPROCESS",$out);
return $out;

After adding this debugging output, check the content displayed when you run your script. Ensure that the content appears as expected with proper line breaks and formatting.
It seems like the issue might be related to how the content is being processed in the get_textprocess function. Let's try a different approach to ensure the content is being handled correctly.

Instead of concatenating each line into a single string in the get_textprocess function, you can directly return the array of lines. Then, in the main script, you can process each line individually within the foreach loop.

$textone = get_textprocess($ac);

foreach ($textone as $aba => $aca) {
$log .= " TEXTONE '$aba' '$aca'\n";
$log .= "CHANNEL '$channel'\n";


function get_textprocess($source) {
$source = trim($source);
$text = file($source, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Read file into an array, ignoring empty lines

return $text;

This way, each line of the file will be an element in the array $textone, and you can loop through them using foreach as intended. Additionally, by using FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES as the second argument to the file() function, empty lines will be skipped, which might help in cleaning up the input.