【发布时间】:2016-01-28 19:34:57
【问题描述】:
我想创建一个插件,用于查找电话号码并将其替换为移动电话的链接号码。这是我为 Joomla 编写的用于替换电话号码的 php 函数...
protected function clickToCall(&$text, &$params){
// phone number pattern...
$pattern = '~(\+0?1\s)?\(?\d{3}\)?[\s.-]\d{3}[\s.-]\d{4}~';
//replacement pattern...
$replacement = '<a href="tel:$1$2">$1$2</a>';
//use preg_replace to actually replace the pattern
$text = preg_replace($pattern, $replacement, $text);
//return the new value
return true;
}
现在该函数找到了该模式并将其替换为一个空链接。如何将正则表达式找到的电话号码插入链接?
【问题讨论】:
-
尝试将
'<a href="tel:$1$2">$1$2</a>'替换为'<a href="tel:$0">$0</a>'。检查this demo。