Forum Moderators: coopster
$count = str_word_count( $sentence ); // 5 words
$list = str_word_count( $sentence, 1 ); // [0]the [1]black [2]bat [3]went [4]onward
for( $loop = 0; $loop < $count; $loop++ ) {
if(!preg_match( '/(and夷f宇he夷n她f地t宇o她n)/', $list[ $loop ] ) ) {
$word_list .= ' '. $list[ $loop ] );
}
}
echo $list // black went
It will skip the word bat because of the "at" and it will skip the word onward because of the "on"
How do I avoid this?
You just need to define 'begins with' and 'ends with' in your regular expression:
if(!preg_match( '/^(and夷f宇he夷n她f地t宇o她n)$/', $list[ $loop ] ) ) {
^ = the begining of a line/group
$ = the end of a line/group
The way it was without the start and end defined, it would match anywhere in a string.
Justin