我想在php字符串中创建一个正则表达式

问题描述 投票:0回答:1

我想要一个用于php字符串的正则表达式..$ string =“提醒我晚上5天后去教堂”;

我想检查字符串中是否存在“ 5天后”之类的内容。如果存在,那么我想要一个变量中的数字5。$ number = 5;

我已经尝试过下面的代码。


if (preg_match("/[after ]+[0-9]+[ days]/", $string)) {
echo "$string is a valid";
}
else
{
  echo "$string is NOT a valid";
}

最终这是我想要实现的。

    if(strpos($string, 'after * days') == true){
        $date = date('Y-m-d',strtotime("+$number days"));
    }
php regex preg-match
1个回答
0
投票

尝试一下

<?php
$pattern = "/\s/";
$replacement = "-";
$text = "Earth revolves around\nthe\tSun";
// Replace spaces, newlines and tabs 
echo preg_replace($pattern, $replacement, $text);
echo "<br>";
// Replace only spaces
echo str_replace(" ", "-", $text);
?>
© www.soinside.com 2019 - 2024. All rights reserved.