Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.97 KB | None | 0 0
  1. <?php
  2.  
  3. require_once "contact.php";
  4. const DATA_FILE = "data.txt";
  5.  
  6. function addContact($contactToAdd) {
  7. $contacts = getContacts();
  8.  
  9. $exist = false;
  10. foreach ($contacts as $contact) {
  11. if($contact->phone === $contactToAdd->phone) {
  12. $exist = true;
  13. break;
  14. }
  15. }
  16. if (!$exist) {
  17. $serializedContact = base64_encode($contactToAdd->firstName) . ";"
  18. . base64_encode($contactToAdd->lastName) .
  19. ";" . base64_encode($contactToAdd->phone) . PHP_EOL;
  20. file_put_contents(DATA_FILE, $serializedContact, FILE_APPEND);
  21.  
  22. }
  23. }
  24. function getContacts(){
  25. $lines = file(DATA_FILE);
  26. $contacts = [];
  27. foreach ($lines as $line) {
  28. list($firstName, $lastName, $phone) = explode(";", $line);
  29. $contact = new contact(base64_decode($firstName),
  30. base64_decode($lastName), base64_decode($phone));
  31. $contacts[] = $contact;
  32. }
  33. return $contacts;
  34. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement