LinkTel-Talk

Summary: Talk page for LinkTel.
Maintainer: Petko
Users: (View? / Edit)

This space is for User-contributed commentary and notes. Please include your name and a date along with your comment.

[Solved] Autodetect telephone numbers

Is it possible to define, in config.php, localised phone number patterns and have the tel: link automatically applied?
e.g. [(\+[0-9]{1,2}\b?)?([0-9]{1,3}\b?)([0-9]{3,4}\b?){2}]

This pattern has an optional country code (1 to 3 digits), an optional area code (1 to 3 digits), and a subscriber number (6 to 8 digits), blanks are optional but some number groupings are expected There would also be a need to define the default country code and area code

simon

You can probably enable this if you always use the same patterns, as described above:

  $DetectTel = array(
    'CountryCode' => '+64',
    'AreaCode' => '04',
  );
  Markup('autotel', '>links', '/
    # not preceded by [, :, =, #, apos, quote
    (?<![:\\["\'=#])
    (?:
      # optional country code
      (\\+\\d{1,3}[\\ -]?)? 
      # optional area code, optionally in parentheses
      \\(?(\\d{1,3})\\)?[\\ -]?
    )?
    # subscriber number, may be split in the middle
    (\\d{3,4}[ -]?\\d{3,4})
    /x', 'DetectTelFmt');

  function DetectTelFmt($m) {
    global $DetectTel;

    $ccode = @$m[1]? $m[1] : $DetectTel['CountryCode']; # add country code if unset

    $acode = @$m[2]? $m[2] : $DetectTel['AreaCode']; # area code if unset
    if(!@$m[1]) $acode = preg_replace('/^0/','',$acode); # trim area code leading zero

    $sn = str_replace (' ', '', $m[3]); # add number, removing embedded spaces

    return Keep("<a class='tel' href='tel:$ccode$acode$sn'>{$m[0]}</a>");
  }

This is now enabled on this page, you can test it here:

* %color=#009900%Call me%% on 021 1234 5678.
** The tel: link above will actually dial +64 21 1234 5678 with the leading zero of the area code replaced with the country code from @@$DetectTel['CountryCode']@@.
* Or call the office at (04) 123 4567. 
** I could simply write 123 4567 if that area code is defined in @@$DetectTel['AreaCode']@@.
* Some number in France: +336 1234 5678 (don't call them if they lose the FIFA world cup match tonight).

These shouldn't be affected (already tel: links):
* tel:12345678
* tel:021-2121-2222
* [[tel:021 2121 2222|Click to dial]]
* [[(tel:)021 2121 2222]]
* [@04 9765 432@]
* [=021 021 021=]
  • Call me on 021 1234 5678.
    • The tel: link above will actually dial +64 21 1234 5678 with the leading zero of the area code replaced with the country code from $DetectTel['CountryCode'].
  • Or call the office at (04) 123 4567.
    • I could simply write 123 4567 if that area code is defined in $DetectTel['AreaCode'].
  • Some number in France: +336 1234 5678 (don't call them if they lose the FIFA world cup match tonight).

These shouldn't be affected (already tel: links):

Note that shortly after I wrote the LinkTel recipe for a project, I had to disable the autodetect/autoprefix feature because of false positives - there would be dates, years, times, room numbers and other numeric strings that got wrongly converted to tel: links. --Petko

1.25 years later: This detection may work TOO well if you have attachments with filenames that look like telephone numbers, and break the links or fail the embeds. This applies to regular Attach: links, and to Maxi links, and possibly for ThumbList with "usetemplate". Not sure how to fix this, but on a wiki I have disabled this for the "imgtpl" action. --Petko

Talk page for the LinkTel recipe (users?).