Skip to content

Instantly share code, notes, and snippets.

@taisang1996
Created July 5, 2017 14:27
Show Gist options
  • Save taisang1996/a20c869fed8b252fa85d7e955ef23a42 to your computer and use it in GitHub Desktop.
Save taisang1996/a20c869fed8b252fa85d7e955ef23a42 to your computer and use it in GitHub Desktop.
<?php
/**
* Make phone clickable
* @param string $input
* @param boolean $span
* @return string
*/
function makePhoneClickable($input, $span = false) {
$regex = '/(?!\s)[\d\s\.\,\(\)\+]{9,30}\b/';
$formatLink = function($matches) {
return "<a href=\"tel:$matches[0]\">$matches[0]</a>";
};
$formatLinkSpan = function($matches) {
return "<a href=\"tel:$matches[0]\"><span>$matches[0]</span></a>";
};
$format = $span ? $formatLinkSpan : $formatLink;
return preg_replace_callback($regex, $format, $input);
}
$input = <<<TEXT
- Số mình là 0912 123 123
- đây +84 942111111
- 01234 56789
- 0956.456.789
- 0923 23 23 23
- 0123 45 67 89 00
- 01234432132
- 0934064004
- 0613985403
- 061 3985403
- +84 1234 4321 32
- +84 01234 4321 32
- +84 93 406 4004
- +84 093 406 4004
- (+84) 1234 4321 32
- (+84) 01234 4321 32
TEXT;
var_dump($input);
var_dump(makePhoneClickable($input));
var_dump(makePhoneClickable($input, true));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment