1

我有以下代码作为 preg_replace 中的替换

"<div style="font-style:italic;margin:8px 6px">$2</div>"

有没有办法将 htmlentities() 包装在 2 美元左右?

4

1 回答 1

3

您可以使用preg_replace_callback()

function replace($matches) {
    return '<div style="font-style:italic;margin:8px 6px">' 
      . htmlentities($matches[2]) . '</div>';
}

preg_replace_callback('/pattern/', 'replace', $string);
于 2013-08-20T00:16:30.887 回答