0

我对 preg_match 不是很好,所以如果这很容易,请原谅我。到目前为止,我已经...

preg_match("/".$clear_user."\/[0-9]{10}/", $old_data, $matches)

我试图匹配的东西看起来像..

:用户名/时间()

现在,preg_match 会出现 22/1266978013 和 2/1266978013 的问题。我需要弄清楚如何匹配冒号。

此外,有没有办法匹配所有数字,直到下一个冒号,而不是只匹配接下来的 10 个数字,因为 time() 可能大于或小于 10。

4

3 回答 3

2

试试这个作为你的模式:

#:$userId/[0-9]+#

preg_match("#:$userId/[0-9]+#", $old_data, $matches);
于 2010-02-24T01:43:49.430 回答
0

您需要扩展匹配并在模式中包含 : 分隔符。不这样做会导致您已经经历过的不稳定行为。

顺便说一句,它并没有那么不稳定:考虑您提交的两个案例:22/1266978013 和 2/1266978013。正则表达式引擎匹配 :2(2/1266978013):(2/1266978013) 您的模式两次。如果您理解字段分隔符 (:),您可以确信只有预期的目标会受到影响。

我会使用 preg_replace 直接替换您找到的模式,一旦您启动昂贵的正则表达式引擎,对我来说,您应该让它执行尽可能多的工作。

$pattern='#:'.$clear_user.'/[0-9]{10}#';
$replacement = ":$clear_user/$new_time"
$last_message=preg_replace($pattern, $replacement, $old_data ,-1,$matches);
if (!$matches) {
  $last_message .= $replacement;
}
于 2010-02-24T02:05:29.163 回答
0
preg_match("/:*".$clear_user."\/[0-9]{10}/", $old_data, $matches);
于 2010-02-24T01:46:55.590 回答