如何在字符串中搜索模式?

时间:2012-04-03 08:53:02

标签: php string

我正在搜索字符串以查找某种模式。

$string = "Monday: 08.00-18.00";
$pattern = "xx.xx-xx.xx";

“x”可以是“0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”或“9”。 “ - ”必须是“ - ”。 “。”可能 ”。”或“:”。

有没有办法解决这个问题?

3 个答案:

答案 0 :(得分:3)

您应该使用preg_match 将x替换为[0-9]
替换。与[.:]

答案 1 :(得分:1)

你想要像

这样的东西
$string = "Monday: 08.00-18.00";
$pattern = "/[0-9]{2}[:|\.][0-9]{2}[-][0-9]{2}[:|\.][0-9]{2}/";
if(preg_match($pattern,$string,$matches))
{
    // has the pattern, do something
    //$matches has all the matches from preg_match
    echo $matches[0]; // prints the matched pattern
}

答案 2 :(得分:0)

这应该是你需要的:

preg_match('/^[0-9]{2}[:.][0-9]{2}-[0-9]{2}[:.][0-9]{2}$/D', $string)
相关问题