至於計數的次數的詞「一」用在字符串中使用快速/簡單:
$sent = "Do you have a different language or operating system? JavaScript is currently disabled in your browser and is required to";
if(preg_match('/ a /', $sent, $matches)) { # a space before and after makes it a word not a letter.
echo count($matches);
}
但是,在所有的情況下,仍然不會告訴你有多少句話是肯定的;要做到這一點需要一個非常複雜的正則表達式。
- >編輯:
得到了這個詞「一」在句子的開頭和其他地方,你可以這樣做:
$sent = "A different language or operating system? JavaScript is currently disabled in your browser and is required to eat a walrus";
$patterns = array('/ a /', '/A /');
$ctr = 0;
foreach($patterns as $p) {
if(preg_match($p, $sent, $matches)) {
$ctr += count($matches);
}
}
echo $ctr;
我瘦你正在尋找[substr_count()](http://php.net/manual/en/function.substr-count.php) – 2013-03-03 21:31:05