如何使用字符类在PHP中搜索字符串


How to use character classes in searching a string in PHP

使用for循环,我想循环遍历字符串中的每个字符并检查它是否是某个字母。假设我想在字符串中搜索我最喜欢的字母——A C D O V。假设我有一个字符串$giantButtText。为什么在我的标准输出中没有输出(假设$giantButtText确实包含这些字母)?

if($giantButtText[$i] == "/[acdov]/") echo $giantButtText[$i];

干杯!

您正在尝试将$giantButtText[$i]匹配到正则表达式。标准的方法是preg_match() (http://php.net/manual/en/function.preg-match.php)。

应该这样做:

$a = array();
$a[0] = "dadov";
if (preg_match("/[acdov]/", $a[0])) echo "true";
-> true