function get_contact($array, $extra_items = [], $has_org = false) { if (!is_array($array)) { return []; } $items = ['fax..:' => 'fax', 'fax.' => 'fax', 'fax-no:' => 'fax', 'fax -' => 'fax', 'fax-' => 'fax', 'fax::' => 'fax', 'fax:' => 'fax', '[fax]' => 'fax', '(fax)' => 'fax', 'fax' => 'fax', 'tel. ' => 'phone', 'tel:' => 'phone', 'phone::' => 'phone', 'phone:' => 'phone', 'phone-' => 'phone', 'phone -' => 'phone', 'email:' => 'email', 'e-mail:' => 'email', 'company name:' => 'organization', 'organisation:' => 'organization', 'first name:' => 'name.first', 'last name:' => 'name.last', 'street:' => 'address.street', 'address:' => 'address.street.', 'language:' => '', 'location:' => 'address.city', 'country:' => 'address.country', 'name:' => 'name', 'last modified:' => 'changed']; if (is_array($extra_items) && count($extra_items)) { foreach ($items as $match => $field) { if (!isset($extra_items[$match])) { $extra_items[$match] = $field; } } $items = $extra_items; } while (list($key, $val) = each($array)) { $ok = true; while ($ok) { reset($items); $ok = false; while (list($match, $field) = each($items)) { $pos = strpos(strtolower($val), $match); if ($pos === false) { continue; } $itm = trim(substr($val, $pos + strlen($match))); /** * @todo Get rid of eval */ if ($field != '' && $itm != '') { eval('$r' . getvarname($field) . '=$itm;'); } $val = trim(substr($val, 0, $pos)); if ($val == '') { unset($array[$key]); break; } else { $array[$key] = $val; $ok = true; } //break; } if (preg_match("/([+]*[-\\(\\)\\. x0-9]){7,}/", $val, $matches)) { $phone = trim(str_replace(' ', '', $matches[0])); if (strlen($phone) > 8 && !preg_match('/[0-9]{5}\\-[0-9]{3}/', $phone)) { if (isset($r['phone'])) { if (isset($r['fax'])) { continue; } $r['fax'] = trim($matches[0]); } else { $r['phone'] = trim($matches[0]); } $val = str_replace($matches[0], '', $val); if ($val == '') { unset($array[$key]); continue; } else { $array[$key] = $val; $ok = true; } } } if (preg_match('/([-0-9a-zA-Z._+&\\/=]+@([-0-9a-zA-Z]+[.])+[a-zA-Z]{2,6})/', $val, $matches)) { $r['email'] = $matches[0]; $val = str_replace($matches[0], '', $val); $val = trim(str_replace('()', '', $val)); if ($val == '') { unset($array[$key]); continue; } else { if (!isset($r['name'])) { $r['name'] = $val; unset($array[$key]); } else { $array[$key] = $val; } $ok = true; } } } } if (!isset($r['name']) && count($array) > 0) { $r['name'] = array_shift($array); } if ($has_org && count($array) > 0) { $r['organization'] = array_shift($array); } if (isset($r['name']) && is_array($r['name'])) { $r['name'] = implode($r['name'], ' '); } if (!empty($array)) { if (isset($r['address'])) { $r['address'] = array_merge($r['address'], $array); } else { $r['address'] = $array; } } return $r; }
function get_contact($array, $extra_items = '') { if (!is_array($array)) { return array(); } $items = array('fax..:' => 'fax', 'fax.' => 'fax', 'fax -' => 'fax', 'fax-' => 'fax', 'fax::' => 'fax', 'fax:' => 'fax', '[fax]' => 'fax', '(fax)' => 'fax', 'fax' => 'fax', 'tel.' => 'phone', 'phone::' => 'phone', 'phone:' => 'phone', 'phone-' => 'phone', 'phone -' => 'phone', 'email:' => 'email', 'e-mail:' => 'email', 'company name:' => 'organization', 'first name:' => 'name.first', 'last name:' => 'name.last', 'street:' => 'address.street', 'language:' => '', 'location:' => 'address.city', 'country:' => 'address.country', 'name:' => 'name'); if ($extra_items != '') { $items = array_merge($extra_items, $items); } while (list($key, $val) = each($array)) { $ok = true; while ($ok) { reset($items); $ok = false; while (list($match, $field) = each($items)) { $pos = strpos(strtolower($val), $match); if ($pos === false) { continue; } $itm = trim(substr($val, $pos + strlen($match))); if ($field != '' && $itm != '') { eval("\$r" . getvarname($field) . "=\$itm;"); } $val = trim(substr($val, 0, $pos)); if ($val == '') { unset($array[$key]); } else { $array[$key] = $val; $ok = true; } break; } } if ($val == '') { continue; } if (!preg_match("/[^0-9\\(\\)\\-\\.\\+ ]/", $val) && strlen($val) > 5) { if (isset($r['phone'])) { $r['fax'] = $val; } else { $r['phone'] = $val; } unset($array[$key]); continue; } if (strstr($val, '@')) { $val = str_replace("\t", ' ', $val); $parts = explode(' ', $val); $top = count($parts) - 1; $r['email'] = str_replace('(', '', $parts[$top]); $r['email'] = str_replace(')', '', $r['email']); array_pop($parts); $val = trim(implode(' ', $parts)); if (strlen($val) < 2) { unset($array[$key]); continue; } $r['name'] = $val; unset($array[$key]); if ($key == 1 && isset($array[0])) { $r['organization'] = $array[0]; unset($array[0]); } } } if (!isset($r['name']) && count($array) > 0) { $r['name'] = array_shift($array); } if (isset($r['name']) && is_array($r['name'])) { $r['name'] = implode($r['name'], ' '); } if (!empty($array) && !isset($r['address'])) { $r['address'] = $array; } return $r; }