Thread: PHP Coders
View Single Post
Old 03-04-2007, 05:56 AM  
nation-x
Confirmed User
 
nation-x's Avatar
 
Industry Role:
Join Date: Mar 2004
Location: Rock Hill, SC
Posts: 5,370
Quote:
Originally Posted by k0nr4d View Post
err nm sorry im drunka nd didn't read entire post.

First off you wanna do file_get_contents() to read the textfile in,
Then explode(); on \n's
then foreach($array as $i) { $var = str_replace('variable',$i,$originalstring); }
PHP has a built in function for that http://www.php.net/file that reads the file and puts each line of the file into an array element.

if you have to files that are exactly the same length in lines then you just have to read them both in... iterate the arrays... make your replacements and output the result.

Code:
$file1 = file('path_to_first_file');
$file2 = file('path_to_second_file'); //assuming that this file contains delimited text for the purpose of this example

$line_iterator = 0;
foreach ($file1 as $line) {
    if (!empy($line)) {
        list($var1, $var2, $var3) = explode(',', $file2[$line_iterator]); //pull variable from corresponding line of file #2 that are comma delimited
        $temp_line = str_replace('<{var1}>', $var1, $line);
        $temp_line = str_replace('<{var2}>', $var2, $temp_line);
        $temp_line = str_replace('<{var3}>', $var3, $temp_line);
        $output .= $temp_line; //add the line to the output
        unset($temp_line);
    }
    $line_iterator++;
}
echo $output;

Last edited by nation-x; 03-04-2007 at 05:57 AM..
nation-x is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote