在字符串中搜索格式化日期并将其替换为当前日期 PHP



我需要在字符串内搜索并将其替换为当前date

这是我试过的。它工作正常,它将日期替换为当前日期,但问题是,我需要初始日期是动态的,所以我可以使用任何日期。

<?php 
    $string = 'we will be held the event in 12/2017 . and waiting for your ..';
    $anydigit = '12/2017';
    $str = str_replace($anydigit,  date(), $string);
    var_dump ($str);
?>
 <div class="container">
    <div class="row">
        <div class="col-md-12">
            <div class="panel panel-default>
                <div class="panel-body">
                     <p> we will be held the event in 12/2017 . and waiting for your </p>
                </div>
            </div>
        </div>  
    </div>
 </div>

听起来你想要一个正则表达式:/d{2}/d{4}/

您还需要将格式传递给 date

完整代码:

$string   = 'we will be held the event in 12/2017 . and waiting for your ..';
$str      = preg_replace('/d{2}/d{4}/', date('Y/m/d'), $string);
var_dump($str);

eval.in 演示

最新更新