用于特定文本模式的 PHP 正则表达式 [英] PHP regex for a specific pattern of text

查看:55
本文介绍了用于特定文本模式的 PHP 正则表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的网站上,我在正文中插入了一个项目的创建年份,并将其替换为六年前"(或无论多长时间).

所以在我的内容中我有:

我们从 1998 年开始营业,并在 [2011] 年前制作了这种包装设计.

我正在尝试使用正则表达式将 2011 放入一个变量中以供以后搜索和替换,但无法弄清楚.每页只有一个实例.我很擅长搜索和替换,这只是我一直无法理解的正则表达式.

为了解决下面的评论 - 年份是可变的,这就是我想使用正则表达式的原因.

示例

$bodycontent = <p>我们从 1998 年开始营业并在 [2002] 年前制作了这个标志设计.</p>

$bodycontent = <p>我们从 1998 年开始营业并在 [2016] 年前制作了这个网站设计.</p>

所以我把大括号中的年份放入一个变量中,正则表达式为 $then,从当前年份中减去它得到 $age(它被另一个函数转换成一个词)

$bodycontent = str_replace("[".$then."]",$age,$bodycontent)

我试过了

preg_match("[\d{4}]",$bodycontent,$found);

但它返回第一个日期——而不是大括号中的那个.

解决方案

假设这种格式 [2002] years,作为替代你可以使用这个正则表达式:

\[(\d{4})\] 年

说明

<前>\[ # 比赛 [( # 捕获组\d{4} # 匹配 4 位数字) # 关闭捕获组\] # 比赛 ]年 # 匹配 `whitespace`years

然后你可以使用 preg_match 来匹配组中的年份1、计算年差并进行单复数格式化.

例如:

$bodycontent = "<p>我们从 1998 年开始营业,并在 [2002] 年前制作了这个标志设计.</p>";preg_match('/\[(\d{4})\] 年/', $bodycontent, $matches);$years = date('Y') - $matches[1];$result = sprintf("%s 年%s",$年,$年 === 1 ?"": "s");$bodycontent = str_replace($matches[0], $result, $bodycontent);

演示 php 输出

On my website I've inserted the year a project was created in the body and swapping that out with "six years ago" (or however long it was).

So in my content I have:

We've been in business since 1998 and produced this packaging design [2011] years ago.

I'm trying to use regex to put 2011 into a variable to later search and replace, and can't figure it out. There's only going to be one instance per page. I'm fine with searching and replacing, it's just regex I've never been able to get my head around.

To address comments below - the year is variable, that's why I'm wanting to use regex.

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>

So I put the year held in braces into a variable with regex as $then, subtract that from the current year to make $age (which is converted into a word by another function)

$bodycontent = str_replace("[".$then."]",$age,$bodycontent)

I've tried

preg_match("[\d{4}]",$bodycontent,$found); 

but it returns the first date — not the one in braces.

解决方案

Assuming this format [2002] years, as an alternative to you might use this regex:

\[(\d{4})\] years

Explanation

\[      # Match [
(       # Capturing group
  \d{4} # Match 4 digits
)       # Close capturing group
\]      # Match ]
 years  # Match `whitespace`years

Then you could use preg_match to match the year in group 1, calculate the difference in years and perform formatting for singular or plural.

For example:

$bodycontent = "<p>We've been in business since 1998 and produced this logo design [2002] years ago.</p>";

preg_match('/\[(\d{4})\] years/', $bodycontent, $matches);
$years = date('Y') - $matches[1];
$result = sprintf("%s year%s",
    $years,
    $years === 1 ? "": "s"
    );
$bodycontent =  str_replace($matches[0], $result, $bodycontent);

Demo php output

这篇关于用于特定文本模式的 PHP 正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆