Forum Moderators: coopster

Message Too Old, No Replies

preg_match skipping too many words

how to avoid

         

twist

10:06 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$sentence = "the black bat went onward";

$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?

jd01

10:22 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi Twist,

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

twist

11:05 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks jd01