Reputation: 57
I have a text file with phone numbers as follows:
7046382696 7046482666 70463826344
I want to write the numbers to a new file. I want each number written on a new line and with a zero before each number, thus:
07046387666 07046382669 08046382693.
This is my script. It doesn't work at all:
//Modify title to fit in url for Search Engine Optimization
function modified_for_new_line($data, $to_remove, $to_replace_with, $added_data)
{
$data_splitter = explode($to_remove, $data);
$data_packer = array_splice($data_splitter, 0);
$modified_data = $added_data . implode($to_replace_with, $data_packer);
return $modified_data;
}
$file1 = fopen("source.txt", "r");
$file2 = '#file2.txt';
if($file1)
{
while(($line = fgets($file1)) !== false)
{
$the_line = modified_for_new_line($line, ' ', "\n", '');
$new_line = 0 . $the_line;
file_put_contents($file2, $new_line);
}
fclose($file1);
fclose($file2);
echo 'All iz well';
}
else {
echo 'Sorry! There was a problem opening the file.';
}
Upvotes: 0
Views: 3764
Reputation: 33823
Using file
to open the source file will give you an array, with each line being a member of the array so it is simple to then do a foreach
on each member.
$source='/path/to/source.txt';
$target='/path/to/target.txt';
$lines=file( $source );
$data=array();
foreach( $lines as $line ){
/* store each line with a leading zero into a temp array */
$data[]='0' . trim( $line );
}
/* write the content back to another file */
file_put_contents( $target, implode( PHP_EOL, $data ) );
Upvotes: 1
Reputation: 55
<?php
$get_file = file_get_contents('file_url.php');
file_put_contents('your_new_file_name.php', $get_file);
?>
that code get file url and copy code and put It in your new file
Upvotes: 2