コード例 #1
0
<?php

function parseContacts($filename)
{
    $handle = fopen($filename, 'r');
    $contents = fread($handle, filesize($filename));
    fclose($handle);
    $contentsArray = explode("\n", $contents);
    $contacts = [];
    $indivContacts = [];
    foreach ($contentsArray as $contact) {
        $tempIndivContacts = explode('|', $contact);
        $indivContacts['name'] = $tempIndivContacts[0];
        $indivContacts['phone'] = substr_replace($tempIndivContacts[1], '-', 3, 0);
        $indivContacts['phone'] = substr_replace($indivContacts['phone'], '-', 7, 0);
        array_push($contacts, $indivContacts);
    }
    return $contacts;
}
var_dump(parseContacts('contacts.txt'));
コード例 #2
0
<?php

function parseContacts($filename)
{
    $contactsArray = array();
    // todo - read file and parse contacts
    $handle = fopen($filename, 'r');
    $contentString = trim(fread($handle, filesize($filename)));
    $arrayOfStrings = explode(PHP_EOL, $contentString);
    foreach ($arrayOfStrings as $index => $contact) {
        $innerArray = explode('|', $contact);
        unset($arrayOfStrings[$index]);
        $contactsArray[$index]['name'] = $innerArray[0];
        $contactsArray[$index]['number'] = numberSplit($innerArray[1]);
    }
    fclose($handle);
    return $contactsArray;
}
function numberSplit($number)
{
    return substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6);
}
print_r(parseContacts('contacts.txt'));
コード例 #3
0
// format is as an multidimensional array with unique keys for the inner array.
function formatContact($array)
{
    foreach ($array as $index => $value) {
        $valueArray = explode('|', $value);
        unset($array[$index]);
        $array[$index]['name'] = $valueArray[0];
        $array[$index]['number'] = formatPhone($valueArray[1]);
    }
    return $array;
}
// Takes a 10 digit string and formats it as a phone number with dashes.
function formatPhone($num)
{
    return substr($num, 0, 3) . '-' . substr($num, 3, 3) . '-' . substr($num, 6);
}
// Takes an external file and parses the info into an array.
function parseContacts($filename)
{
    $contacts = array();
    // $filename = 'data/contacts.txt'; if not in a function, this variable would be needed in place of the parameter.
    $handle = fopen($filename, 'r');
    $contents = fread($handle, filesize($filename));
    $contentsString = trim($contents);
    $contentsArray = explode("\n", $contentsString);
    fclose($handle);
    $contacts = formatContact($contentsArray);
    return $contacts;
}
var_dump(parseContacts('data/contacts.txt'));
コード例 #4
0
<?php

function parseContacts($filename)
{
    $contactsArray = array();
    // todo - read file and parse contacts
    $handle = fopen($filename, 'r');
    $contentString = fread($handle, filesize($filename));
    $contentString = trim($contentString);
    $arrayOfStrings = explode("\n", $contentString);
    return $contentString;
}
var_dump(parseContacts('/Users/Roger/vagrant-lamp/sites/codeup.dev/public/php/contacts.txt'));
コード例 #5
0
ファイル: contact-parser.php プロジェクト: Reni789/Exercises
<?php

function parseContacts($filename)
{
    $contacts = array();
    $handle = fopen($filename, 'r');
    $contents = fread($handle, filesize('data/contacts.txt'));
    $contents = trim($contents);
    $contentsArray = explode("\n", $contents);
    foreach ($contentsArray as $value) {
        $new_Array = explode("|", $value);
        $name = $new_Array[0];
        $phonenumber = $new_Array[1];
        // $phonenumber = substr($phonenumber, 0, 3) . '-' . substr($phonenumber, 3, 3) . '-' . substr($phonenumber, -4);
        $contacts[] = ["name" => $name, "number" => $phonenumber];
    }
    fclose($handle);
    return $contacts;
}
$new = parseContacts('data/contacts.txt');
var_dump($new);
コード例 #6
0
<?php

function parseContacts($filename)
{
    $contacts = array();
    $handle = fopen($filename, 'r');
    $contents = fread($handle, filesize($filename));
    $contents = trim($contents);
    $contacts = explode("\n", $contents);
    fclose($handle);
    foreach ($contacts as $index => $contact) {
        $contact = explode('|', $contact);
        unset($contacts[$index]);
        // $number = substr_replace($number, '-', 3, 0);
        // $number = substr_replace($number, '-', 7, 0);
        // $contacts[$index]['name']   = $contact[0];
        // $contacts[$index]['number'] = numberSplit($contact[1]);
        $contacts[$index] = ['name' => $contact[0], 'number' => numberSplit($contact[1])];
    }
    return $contacts;
}
$contacts = parseContacts('contacts.txt');
/**
 * Takes an unformated phone number and returns a formated string;
 */
function numberSplit($number)
{
    return substr($number, 0, 3) . '-' . substr($number, 3, 3) . '-' . substr($number, 6);
}
print_r($contacts);