执行此操作的正确方法是什么?
<?php
$word = 'dogcatdog';
preg_match( '/do'."[^0-9]".'atdog/i', $word, $matches );
print_r($matches);
?>
我想返回"gc“。
我一直收到一个错误:“未知修饰符'['”。
发布于 2011-02-05 08:39:00
如果两只狗围绕着一只猫,我们首先可以观察到的是,猫会开始发出很多叫声,这会产生一个"Hsssss“。然后狗就会开始狂吠和咆哮,这就给了我们一声“格格格”的叫声。因此,这将产生类似于"Hsssss Grrr Grrr Hsssss BARK Hsssss Grrr“的内容。所以现在,记住我们想要"gc“作为我们的最终结果,我们应用下面的代码
$word = 'dogcatdog';
preg_match("[r]+", $word, $matches);
preg_match("(BARK)", $word, $matches);
preg_match("[Hs]+", $word, $matches);
$matches = "c";
preg_match("[G]+", $word, $matchesFinal);
$matches = $matchesFinal[0] . $matches;
太棒了!
发布于 2011-02-05 08:27:36
preg_match( '/do([a-z]{2})atdog/i', $word, $matches );
如果你想找到gc
,你永远不会去做[^0-9]
。这将只查找任何非数字字符,因此将包括所有标点符号,以及除0..9之外的任何内容。
当然,如果您真的只想匹配字符串中的gc
,您就会这样做。
preg_match( '/do(gc)atdog/i', $word, $matches );
然而,这似乎相当没有意义。
发布于 2011-02-05 08:26:53
我不知道具体的PHP语法,但试试这个:
preg_match( '/do([^0-9]+)atdog/i', $word, $matches );
https://stackoverflow.com/questions/4904237
复制相似问题