PDA

View Full Version : تبدیل preg_replace به preg_replace_callback



mehdisat2000
یک شنبه 20 مرداد 1398, 23:54 عصر
سلام.
من یه کد دارم که تاریخ میلادی رو برای یه اسکریپت به شمسی تبدیل میکنه اما بعد از آپدیت نسخه PHP دیگه کار نمیکنه
preg_replace(): The /e modifier is no longer supported, use preg_replace_callback instead.


اومدم تغییرات رو اعمال کنم و تبدیل preg_replace به preg_replace_callback رو انجام بدم که متاسفانه نشد.

بخشی از کد که ارور مربوط به اونه به صورت زیره:

$pattern=array(
"#Y#",//full year
"#y#",//short year

"#M#",//month short name
"#F#‎‎‎‎‎‎",//month full name
"#m#",//month number 0 lead
"#n#",//month number
"#t#",//days in month

"#l#",//full week day
"#D#",//short week day

"#d#",//day number of month
"#j#",//day number of month

"#a#",//AM/PM short view
"#A#",//AM/PM full view

"#([^yYMmDdAa])#e"
);
$replace=array(
$d->ENnum2FA($converted[0]),//year 13xx
$d->ENnum2FA(substr($converted[0],2),true),//year xx lead zero

$d->shmonths[$converted[1]],//month name
$d->months[$converted[1]],//month name
$d->ENnum2FA($converted[1],true), //month number
$d->ENnum2FA($converted[1]), //month number
//$converted[1],
$d->j_days_in_month[$converted[1]],

$d->days[strtolower(gmdate("D",$stamp))],//week day {full view}
$d->ldays[strtolower(gmdate("D",$stamp))],//week day ‍‍{short view}

$d->ENnum2FA($converted[2],true),//day of month
$d->ENnum2FA($converted[2],true),//day of month

$d->pmam[gmdate('a',$stamp)],
$d->pmam[gmdate('A',$stamp)],

"\$d->ENnum2FA(gmdate('\\1',\$stamp))"
);

$date= preg_replace($pattern,$replace,$format);

من با توجه به آموزش بعضی از سایتا تغییرات زیر رو اعمال کردم:

1. "#([^yYMmDdAa])#e" ===> "#([^yYMmDdAa])#"

2. "\$d->ENnum2FA(gmdate('\\1',\$stamp))" ===> "\$d->ENnum2FA(gmdate('\$match[1]',\$stamp))"

3. $date= preg_replace($pattern,$replace,$format); ===> $date=preg_replace_callback($pattern, function($match) {
return $replace;
}, $format);

در نهایت کد به صورت زیر شد اما هنوز کار نمیکنه. به نظرتون مشکل از کجاست؟

$pattern=array(
"#Y#",//full year
"#y#",//short year

"#M#",//month short name
"#F#‎‎‎‎‎‎",//month full name
"#m#",//month number 0 lead
"#n#",//month number
"#t#",//days in month

"#l#",//full week day
"#D#",//short week day

"#d#",//day number of month
"#j#",//day number of month

"#a#",//AM/PM short view
"#A#",//AM/PM full view

"#([^yYMmDdAa])#"
);
$replace=array(
$d->ENnum2FA($converted[0]),//year 13xx
$d->ENnum2FA(substr($converted[0],2),true),//year xx lead zero

$d->shmonths[$converted[1]],//month name
$d->months[$converted[1]],//month name
$d->ENnum2FA($converted[1],true), //month number
$d->ENnum2FA($converted[1]), //month number
//$converted[1],
$d->j_days_in_month[$converted[1]],

$d->days[strtolower(gmdate("D",$stamp))],//week day {full view}
$d->ldays[strtolower(gmdate("D",$stamp))],//week day ‍‍{short view}

$d->ENnum2FA($converted[2],true),//day of month
$d->ENnum2FA($converted[2],true),//day of month

$d->pmam[gmdate('a',$stamp)],
$d->pmam[gmdate('A',$stamp)],

"\$d->ENnum2FA(gmdate('\$match[1]',\$stamp))"
);
$date=preg_replace_callback($pattern, function($match) {
return $replace;
}, $format);

baleghsefat.ir
دوشنبه 21 مرداد 1398, 12:41 عصر
از این استفاده کن برای تبدیل تاریخ:
https://github.com/hekmatinasser/verta