[英]PHP regular expression format text
我需要有關php5正則表達式功能的幫助。 我需要一個函數,該函數接受參數文本,正則表達式,字符串之前,之后。
function Highlight($text, $regex, $before, $after)
這個例子可能是我需要的最好的解釋。
$text="I have apple and banana."
$regex="(apple|banana)"
$before="<b>"
$after="</b>"
我需要退貨
"I have <b>apple</b> and <b>banana<b>."
一個例子只是為了說明問題,它不會是html元素。
謝謝
$text="I have apple and banana.";
$regex="(apple|banana)";
$before="<b>";
$after="</b>";
print preg_replace("@".$regex."@", $before."$1".$after, $text);
function Highlight($text, $regex, $before, $after) {
$pattern = '/'. $regex .'/';
return preg_replace_callback($pattern,
function ($m) use ($before,$after) {
return $before.$m[0].$after;
}
, $text);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.