Home  >  Article  >  Backend Development  >  一个正则加法和减法的运算,解决方法

一个正则加法和减法的运算,解决方法

WBOY
WBOYOriginal
2016-06-13 13:52:181339browse

一个正则加法和减法的运算,
$result= "中华民国96年 ";
$result   =   preg_replace( '/中华民国([0-9]+)年/s ', '公元1912+\\1-1年 ',$result);

行运结果是:公元1912+96-1年


我要的结果是:公元2007年  

怎么才能让 "1912+96-1 "运算结果出来??

------解决方案--------------------
$result= "中华民国96年 ";
echo $result = preg_replace( '/中华民国([0-9]+)年/es ', 'ch(\\1) ',$result);

function ch($s)
{
$y = 1912 + $s - 1;
return '公元 '. $y . '年 ';
}
?>

output:

公元2007年

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn