I am trying to migrate my sites from PHP 5.4 to HHVM. I learned that preg_replace /e was E_DEPRECATED so I tried to convert all occurrences as possible. I got many parts corrected but fail to convert those with pattern in array. The following is one of the case:
if(strpos($msglower, '[/img]') !== FALSE) {
$message = preg_replace(array(
"/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies",
"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/ies"
), $allowimgcode ? array(
"bbcodeurl('\\1', '<img src=\"%s\" border=\"0\" onclick=\"zoom(this, this.src)\" onload=\"attachimg(this, \'load\')\" alt=\"\" />')",
"bbcodeurl('\\3', '<img width=\"\\1\" height=\"\\2\" src=\"%s\" border=\"0\" alt=\"\" />')"
) : array(
"bbcodeurl('\\1', '<a href=\"%s\" target=\"_blank\">%s</a>')",
"bbcodeurl('\\3', '<a href=\"%s\" target=\"_blank\">%s</a>')"
), $message);
}
I knew I should use anonymous function to convert but I am not sure where the function($matchese){return XXXXXXXXXXXX;} should be placed as there are more than one preg_replace /e within the pattern array. I tried to put at the beginning of REPLACEMENT string or before each array element. Both fail.I was stucked at this for two days now. Anyone can help me to solve this?
I tried to amend as your suggested by adding anonymous function blocks at each return array but it will give me empty content at that position:
if(strpos($msglower, '[/img]') !== FALSE) {
$message = preg_replace_callback(array(
"/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is",
"/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is"
), $allowimgcode ? function($v) {return array(
"bbcodeurl('$v[1]', '<img src=\"%s\" border=\"0\" onclick=\"zoom(this, this.src)\" onload=\"attachimg(this, \'load\')\" alt=\"\" />')",
"bbcodeurl('$v[3]', '<img width=\"$v[1]\" height=\"$v[2]\" src=\"%s\" border=\"0\" alt=\"\" />')"
);} : function($v) {return array(
"bbcodeurl('$v[1]', '<a href=\"%s\" target=\"_blank\">%s</a>')",
"bbcodeurl('$v[3]', '<a href=\"%s\" target=\"_blank\">%s</a>')"
);}, $message);
}
This is one of my previous attempt actually. Seems it doesn't work.
As per reminded by axiac that the replacement part must be string and cannot be a array, I tried this and seems work:
if(strpos($msglower, '[/img]') !== FALSE) {
$message = preg_replace_callback("/\[img\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is",
$allowimgcode ? function($v) {return bbcodeurl($v[1], '<img src="%s" border="0" onclick="zoom(this, this.src)" onload="attachimg(this, \'load\')" alt="" />');} :
function($v) {return bbcodeurl($v[1], '<a href="%s" target="_blank">%s</a>');}, $message);
$message = preg_replace_callback("/\[img=(\d{1,4})[x|\,](\d{1,4})\]\s*([^\[\<\r\n]+?)\s*\[\/img\]/is",
$allowimgcode ? function($v) {return bbcodeurl($v[3], "<img width=\"$v[1]\" height=\"$v[2]\" src=\"%s\" border=\"0\" alt=\"\" />");} :
function($v) {return bbcodeurl($v[3], '<a href="%s" target="_blank">%s</a>');}, $message);
}
Thanks a lot guys!