希望有人能帮我解决这个问题。
我正在寻找的是在字符串中搜索单词
$text = 'is Apple, name testintapple test APPLE name test';
preg_match_all('/apple/i', $text, $output_array);
返回
array(
0 => Apple
1 => apple
2 => APPLE
)
我希望它返回:
array(
0 => Apple
1 => APPLE
)
最佳答案
您需要的是:
preg_match_all('/\bapple\b/i', $text, $output_array);
\b
是字边界。因此,仅当 apple
用作单词而不是较大字符串的一部分时,它才会匹配。
关于php - 用于搜索文本中的单词而不是字符串的一部分的正则表达式 (preg_match_all),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68533635/