在我的网站上,我在正文中插入了项目创建的年份,并将其替换为“六年前”(或者无论有多长)。
所以在我的内容中我有:
我们自 1998 年开始营业,并在 [2011] 年前制作了这种包装设计。
我试图使用正则表达式将 2011 放入变量中以便稍后搜索和替换,但无法弄清楚。每页只会有一个实例。我很擅长搜索和替换,这只是我一直无法理解的正则表达式。
为了解决下面的评论 - 年份是可变的,这就是我想使用正则表达式的原因。
example
$bodycontent = <p>We've been in business since 1998
and produced this logo design [2002] years ago.</p>
or
$bodycontent = <p>We've been in business since 1998
and produced this website design [2016] years ago.</p>
因此,我将大括号中的年份放入正则表达式为 $ 的变量中,然后从当前年份中减去该年份以得到 $age (由另一个函数将其转换为单词)
$bodycontent = str_replace("[".$then."]",$age,$bodycontent)
我试过了
preg_match("[\d{4}]",$bodycontent,$found);
但它返回第一个日期——而不是大括号中的日期。