Skip to content

Instantly share code, notes, and snippets.

@cygeorgel
Created April 12, 2020 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cygeorgel/e0b92821fbc78c08108c1200a739799e to your computer and use it in GitHub Desktop.
Save cygeorgel/e0b92821fbc78c08108c1200a739799e to your computer and use it in GitHub Desktop.
public function createContactFromString($string)
{
$data = [
'contactId' => null,
'name' => null,
'email' => null,
'phone' => null,
'mobilePhone' => null,
];
$elements = explode(',', $string);
if (count($elements)) {
foreach ($elements as $element) {
if ( ! $data['email'] && filter_var(str_replace(' ', '', $element), FILTER_VALIDATE_EMAIL) !== false) {
$data['email'] = str_replace(' ', '', $element);
}
if ( ! $data['mobilePhone']) {
if (strlen(filter_var(str_replace(' ', '', $element), FILTER_SANITIZE_NUMBER_INT)) >= 10) {
if (substr(str_replace(' ', '', $element), 0, 2) == '06' || substr(str_replace(' ', '', $element), 0, 2 ) == '07') {
$data['mobilePhone'] = phoneNumberFormat($element);
}
}
}
if ( ! $data['phone']) {
if (strlen(filter_var(str_replace(' ', '', $element), FILTER_SANITIZE_NUMBER_INT)) >= 10) {
if (substr(str_replace(' ' , '', $element), 0, 2) != '06' && substr(str_replace(' ', '', $element), 0, 2 ) != '07') {
$data['phone'] = phoneNumberFormat($element);
}
}
}
if ( ! $data['name']) {
if (strlen(filter_var($element, FILTER_SANITIZE_NUMBER_INT)) == 0) {
if ( ! filter_var($element, FILTER_VALIDATE_EMAIL)) {
$data['name'] = $element;
}
}
}
}
}
return $data;
}
@cygeorgel
Copy link
Author

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment