-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontact-parser.php
87 lines (40 loc) · 1.4 KB
/
contact-parser.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
if (! isset($argv[1])) {
echo 'give me a filename!' . PHP_EOL;
die;
}
$contact = $argv[1];
function getFileContents($filename)
{
// create a resource that points to our file
// http://php.net/manual/en/function.fopen.php
// 'r' means for reading
// - more modes in the next lesson
// like a cursor or pointer
$handle = fopen($filename, 'r');
// our handle is at the start of the file, read until the end of the file
// i.e. the size of the file
$fileContents = fread($handle, filesize($filename));
// make sure to cleanup
fclose($handle);
return $fileContents;
}
$names = getFileContents($contact);
// Turn names into an array//////////////////////////////////////////////////
$names = explode("\n", $names);
// Go through array of strings and split them in to smaller strings/////////
$contacts=array();
foreach ($names as $key => $value) {
$names[$key]=explode("|", $value);
$newArray=array ("name"=>$names[$key][0], "number"=>$names[$key][1]);
array_push($contacts, $newArray);
}
function stringReplace($oldstr,$str_to_insert,$pos){
$str = substr($oldstr, 0, $pos) . $str_to_insert . substr($oldstr, $pos);
return $str;
}
for ($i=0; $i <=2 ; $i++) {
$contacts[$i]["number"]=stringReplace($contacts[$i]["number"],"-",3);
$contacts[$i]["number"]=stringReplace($contacts[$i]["number"],"-",7);
}
var_dump($contacts);