我想創建一個插件,它可以找到並替換手機號碼與手機號碼。這是我寫爲Joomla用更換電話號碼的PHP函數...PHP正則表達式preg_replace函數Joomla!插件
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;
}
眼下函數查找模式,只是一個空鏈接替換它。如何將正則表達式找到的電話號碼插入鏈接?
嘗試用'$0'替換''''。檢查[此演示](http://ideone.com/YUVXNm)。 –