>PHP 5.3:
preg_replace_callback('..pattern...',
function($match){return empty($match[2]) ? $match[1]:$match[2];},
$string);
<PHP 5.3:
function _my_func($match) {
return empty($match[2]) ? $match[1]:$match[2];
}
preg_replace_callback('..pattern...',
'_my_func',
$string);
或者,在旧的PHP版本中,
/e
修饰符有
since 5.5 been
deprecated
)
preg_replace('...pattern..../e','strlen("$2") > 0?"$2":"$1"',$string);