I have a text file of phone numbers like below:
+2348089219281 +2348081231580 +2347088911847 +2347082645764 +2348121718153 +2348126315930 +2348023646683.
I want to extract each number, strip +234
from it and replace with 0
, then add the following text "Names" . "\t"
in front of the modified number.
Then I want to insert this new string in a new text file (line-by-line).. This is what I get in the new_textFile with the code I've wrote:
Names 00urce id #3
Names 00urce id #3
Here's my code:
$this_crap_file = fopen($old_file_name, "r");
$total_number_lines_for_this_crap_file = count($this_crap_file);
while(!feof($this_crap_file))
{
$the_new_writing = fopen($new_file_name, "a");
$the_string = substr_replace($this_crap_file, "0", 0, 4);
$new_string = "Names" . "\t" . 0 . $the_string . "
";
fwrite($the_new_writing, $new_string);
}
fclose($this_crap_file);
No space between fopen and the parenthesis? Sorry, I don't see the relevance of that statement.
Assuming your input file only has one phone number per line, and that they all start with '+234', you could use a regular expression to pick out just the part you want to put in the new file, like this:
$this_crap_file = fopen($old_file_name, "r");
$the_new_writing = fopen($new_file_name, "a");
while ($line = fgets($this_crap_file))
{
preg_match('/\+234(\d+)/', $line, $matches);
$new_string = "Names\t" . $matches[1] . "
";
fwrite($the_new_writing, $new_string);
}
fclose($the_new_writing);
fclose($this_crap_file);