dongmangzong8006 2018-01-05 15:39
浏览 72

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

I need to search inside the string and replace it with the current date

This what i have tried yet. It works fine, it replaces the date into the current date, but the problem is, that I need the initial date to be dynamic, so i can use any 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>
  • 写回答

1条回答 默认 最新

  • dsfds2353 2018-01-05 15:42
    关注

    Sounds like you want a regex: /\d{2}\/\d{4}/

    You also need to pass a format to date.

    Full code:

    $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 demo

    评论

报告相同问题?

悬赏问题

  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥15 Oracle触发器记录修改前后的字段值
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题