dongzhanjuan5141 2018-04-26 10:53
浏览 33
已采纳

PHP - 如果字符串与动态数字匹配,则为if语句

I have a PHP string that looks like this

$string = 'This is day 7 of the task';

The number changes depending on what is happening that week so I am trying to do an if statement to check a number has been set like this

if ($string == 'This is day 7 of the task') {
    echo 'Task day is set to a number';
}

I now want to match it regardless of what the number is, what is my best approach, regex?

Something like this maybe?

$string = 'This is day 7 of the task';
$isAMatch = preg_match("/This is day \(\d)+\ of the task\/", $string);
  • 写回答

5条回答 默认 最新

  • duanshan1511 2018-04-26 10:57
    关注

    You can indeed do something like this :

    if (preg_match("/^This is day [0-9]+ of the task$/", $string)) {
        echo 'Task day is set to a number';
    }
    

    the ^means "starts with" and $ means "ends with". Remove them if your string can contain other char at the start or the end.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程