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);

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.

Your actual regular expression doesn't work because you're escaping the parenthesis \( and the last delimiter \/. If you don't have to get the number, you could use:

$string = 'This is day 7 of the task';
$isAMatch = preg_match('~This is day \d+ of the task~', $string);

If you want to get the number, you could add a capture group:

$string = 'This is day 7 of the task';
$isAMatch = preg_match('~This is day (\d*) of the task~', $string, $matches);
if ($isAMatch) {
    echo $matches[1]; // 7
}

Its possible - as you said - to use regex for this:

<?php
$string = 'This is day 7 of the task';
preg_match('~[0-9]~', $string, $matches);
var_dump($matches);

Output:

array(1) {
[0]=>
    string(1) "7"
}

Now you can follow-up with

if(count($matches) > 0) {
    echo $matches[0]; // 7
}

https://3v4l.org/aiVlV

There is no need for a regex in my opinion.
You can str_replace the words from the string and what you are left with is the number.

This can later be used to compare against what will happen on each day.

$string = 'This is day 7 of the task';
$arr = ["This", "is", "day", "of", "the", "task", " "];

$DayInString = str_replace($arr, "", $string);

echo $DayInString;

https://3v4l.org/LuGGv

If $DayInString is only a digit it's a match and the digit is the day of the week or month or whatever.

I love regular expressions, but they should only be used if there isn't another direct technique to perform the required task. You can check for and extract the expected integer value using filter_var($string, FILTER_SANITIZE_NUMBER_INT) in a condition which also stores the integer to $int in one line.

Code: (Demo)

$string = 'This is day 7 of the task';
if ($int = filter_var($string, FILTER_SANITIZE_NUMBER_INT)) {
    echo "Found \"$int\" inside of $string";
} else {
    echo "No integers found inside of $string";
}

echo "
---
";

$string = 'This is a day of the week';
if ($int = filter_var($string, FILTER_SANITIZE_NUMBER_INT)) {
    echo "Found \"$int\" inside of \"$string\"";
} else {
    echo "No integers found inside of \"$string\"";
}

Output:

Found "7" inside of This is day 7 of the task
---
No integers found inside of "This is a day of the week"