Navigation Menu

Skip to content

Commit

Permalink
normalize telephone number
Browse files Browse the repository at this point in the history
normalize telephone numbers to include all non-numeric outside
class=value

see http://microformats.org/wiki/value-class-pattern
  • Loading branch information
glensc committed Jul 21, 2012
1 parent 468a233 commit e58901f
Showing 1 changed file with 40 additions and 4 deletions.
44 changes: 40 additions & 4 deletions syntax.php
Expand Up @@ -312,17 +312,53 @@ private function _tagclass($tag, $text) {
return $this->_tag($tag, $text, $tag);
}

/**
* normalize telephone number to include all non-numeric outside class=value
* see http://microformats.org/wiki/value-class-pattern
*/
private function _tel_normalize($value) {
$res = array();

// match all but +, digits and space
if (!preg_match_all('/[^+\d\s]+/', $value, $matches, PREG_OFFSET_CAPTURE)) {
$res[] = array('+', $value);
return $res;
}

$offset = 0;
foreach ($matches[0] as $match) {
$v = substr($value, $offset, $match[1] - $offset);
if ($v) {
$res[] = array('+', $v);
}
$res[] = array('-', $match[0]);
$offset = $match[1] + strlen($match[0]);
}
$v = substr($value, $offset);
if ($v) {
$res[] = array('+', $v);
}
return $res;
}

/**
* format hcard telephone numbers
*/
private function _tel(&$renderer, $type, $value) {
// TODO: normalize numbers and use <abbr title> for phone numbers
// see http://microformats.org/wiki/value-class-pattern

$type = '<b>'.$this->_tag('tel_type_'.$type, $type, 'type').'</b> ';
$value = $this->_tag('tel_value', $renderer->_xmlEntities($value), 'value');

return $this->_tagclass('tel', $type.$value);
$values = '';
foreach ($this->_tel_normalize($value) as $res) {
list($t, $value) = $res;
if ($t === '+') {
$values .= $this->_tag('tel_value', $renderer->_xmlEntities($value), 'value');
} else {
$values .= $value;
}
}

return $this->_tagclass('tel', $type.$values);
}

private function _emaillink(&$renderer, $mail, $name = '') {
Expand Down

0 comments on commit e58901f

Please sign in to comment.