用于特定文本模式的PHP正则表达式

时间:2018-02-11 03:10:40

标签: php regex

在我的网站上,我插入了一个项目在体内创建的年份,并将其与“六年前”交换出来(或者不久之后)。

所以在我的内容中我有:

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

我正在尝试使用正则表达式将2011放入变量以便以后搜索和替换,并且无法弄明白。每页只有一个实例。我很适合搜索和替换,它只是正则表达式我从来没有能够解决这个问题。

要解决以下评论 - 年份是可变的,这就是我想要使用正则表达式的原因。

例如

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

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

所以我把括号中的年份变成了一个带有正则表达式的变量作为$ then,从当前年份中减去$ age(由另一个函数转换成单词)

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

我试过

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

但它会返回第一个日期 - 而不是大括号中的日期。

3 个答案:

答案 0 :(得分:0)

使用函数preg_replace_callback()

$bodycontent = preg_replace_callback('~\[(\d{4})\]~', function($match) {
    return (date('Y') - $match[1]) . " ago";
}, $bodycontent);

demo

答案 1 :(得分:0)

如果这是我的项目,我可能会使用preg_replace_callback()调用利用查找数组的单词,并在括号内日期“超出范围”时进行后备替换。

*在适当的时候复制year很重要 *我的模式中的第二个]不需要转义,但如果你觉得它提高了可读性,你可以添加它。
*我也匹配years ago,以便替换文本在所有情况下都有意义;您可能希望删除原始输入文本中的此尾随文本。

代码:(Demo

$bodycontent = "<p>We've been in business since 1998 and produced this logo design [1995] years ago.</p>\n";
$bodycontent .= "<p>We've been in business since 1998 and produced this website design [2018] years ago.</p>\n";
$bodycontent .= "<p>We've been in business since 1998 and produced this website design [2017] years ago.</p>\n";
$bodycontent .= "<p>We've been in business since 1998 and produced this website design [2016] years ago.</p>";

$lookup=['less than a','one','two','three','four','five','six','seven','eight','nine',
         'ten','eleven','twelve','thirteen','fourteen','fifteen','sixteen','seventeen','eighteen','nineteen'
];  // extend as needed

$bodycontent = preg_replace_callback('/\[(\d{4})] years ago/', function($m)use($lookup){
    $diff=date('Y')-$m[1];
    if(isset($lookup[$diff])){
        return $lookup[$diff].' year'.($diff>1?'s':'').' ago';
    }else{
        return "in {$m[1]}";  // out of lookup range, use backup text
    }
}, $bodycontent);

echo $bodycontent;

输出:

<p>We've been in business since 1998 and produced this logo design in 1995.</p>
<p>We've been in business since 1998 and produced this website design less than a year ago.</p>
<p>We've been in business since 1998 and produced this website design one year ago.</p>
<p>We've been in business since 1998 and produced this website design two years ago.</p>

答案 2 :(得分:0)

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

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

<强>解释

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

然后您可以使用preg_match来匹配组1中的年份,计算年份差异并执行单数或复数格式。

例如:

$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