开发者

Convert year to difference as bbcode

So i wanna do something like this in php. If i have a year in brackets [1995] I want it to convert it to the the difference of the current year to 1995.

Something like "It's been [1995] years since something happened."

so with it being开发者_如何转开发 2009, it'd say

"It's been 14 years since something happened."

Obviously the subtracting part would be pretty easy, but I can't figure out how I'd write this so that i can put any number in brackets and the php would output the difference. I'm using this for a calendar, where dozens of dates stay the same each year, just want to note the difference.

I wanna use something like

preg_replace(array_keys($bbcode), array_values($bbcode), $events)

$bbcode array already has some items like this

"/\[url\=(.*?)\](.*?)\[\/url\]/is" => "<a href='$1' target='_blank'>$2</a>",


Try something like this

<?php
$str = "It's been [1995] years sice something...";

$regexp = '/\[(\d+)\]/e';
$replace = 'date("Y", time()) - $1';

echo preg_replace($regexp, $replace, $str);
?>

The e modifier on the regex makes the replace string execute as PHP, rather than a simple string.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜