Jump to content

Text File and IF in WHILE


mikedag

Recommended Posts

Hello,

 

working with a text file but cannot seem to figure out a few things.. first off my code is:

function openInFile ($file) {

$f = fopen($file, 'r');

while ($line = fgets($f, 1000)) {

	$newline = split('\t', $line);

	if ($newline[0] == 'See') {

		echo 'rtv';

	} else {

		echo $newline[0] . '\t' .$newline[1] . '\n';
	}
}

 

I know I should be using preg_split but cannot figure out how to use it... anyway...

 

Each line in the text file looks like:

 

thbyreuo 36

 

with exception of a few lines that only have the word "See" on them.

 

when running the script the out put i get is:

 

See

,anhah esuav udwusthev\t 1101 \n

 

So basically, i guess it does not check if "See" exists to add the replacement and it does not properly output tabs(\t) or newlines (\n). As of right now I am just outputting to the browser and not to a text file. Would this have any affect on the tabs and newlines not appearing?

 

Thanks!

Mike

Link to comment
Share on other sites

Without seeing the "real" data it is difficult to determine the problem. If the line has a space or other "hidden" character you can't do a comparison with "See". Also, '\t' will not produce a tab character when delimited within single quotes - it will only produce the actual characters of '\t'. You need to use double quotes.

 

The code - as written - takes a chunk of data and does a split on the tab character. But, you do not state that there are tabs in the data only that "See" is on some lines by themselves and other lines have two pieces of data which appear to be separated by multiple spaces. It would be helpful to know EXACTLY how those two pieces of data are separated. Are the separated by a tab, by a fixed number of spaces or are they fixed-width? The answer will detemine the best solution.

 

Use file() to read the file into an associative array with each line a separate element in the array

 

http://us2.php.net/manual/en/function.file.php

 

 

This example uses preg_split to split the data line on one or more spaces, but depending on how theY really are delimited this is probably not the best solution. For testing purposes you will want to echo PRE tags before and after calling this function so the HTML output will look like the file created. Otherwise the presentation of the HTML output will not show line breaks.

function openInFile ($file)
{
    $lines = file($file);

    foreach ($lines as $line)
    {
        $line = trim($line);
        if ($line == 'See') {
            echo "rtv\n";
        } else {
            $dataAry = preg_split("/[\s]+/", $line);
            echo "{$dataAry[0]}\t{$dataAry[1]}\n";
        }
    }
}

Link to comment
Share on other sites

When using double quotes to delimit text and variables within the quotes will be interpreted as the value of those vairables. however, in single quoted test the variable is not interpreted:

$foo = 'bar';
echo 'I'm going to the $foo'; //Output I'm going to the $foo
echo "I'm going to the $foo"; //Output I'm going to the bar

 

This works fine as long as there is a space after the variable to be interpreted (or some characters). But, if you need other text to display right after the variable, the PHP parser will not know where the variable name ends. Suppose you have a variable which holds a numeric value for gigabytes of RAM and you want to display it like this: "4GB"

 

This would not work

$ram = '6';
echo "The PC has $ramGB of ram.";
//Output: The PC has  of ram

The parser will think the variable is $ramGB. So, you can further delimit variable in double quotes by putting them in curly braces

 

$ram = '6';
echo "The PC has {$ram}GB of ram.";
//Output: The PC has 6GB of ram

 

Therefore, I always use curly braces around my variables in double quotes as a standard practice. It also helps to make the variables stand out when reviewing code.

Link to comment
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.