Rowno Posted August 1, 2008 Share Posted August 1, 2008 Is it possible to match everything (using .*?) except for a few words or character combinations? Quote Link to comment Share on other sites More sharing options...
effigy Posted August 4, 2008 Share Posted August 4, 2008 Something like this? <pre> <?php echo $str = join('', range('a', 'z')); echo '<hr>'; preg_match('/(??!m).)*/', $str, $matches); print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Rowno Posted August 5, 2008 Author Share Posted August 5, 2008 How can I use that to match any word except ones that I define? Quote Link to comment Share on other sites More sharing options...
effigy Posted August 5, 2008 Share Posted August 5, 2008 See the comment below: <pre> <?php echo $str = 'apple orange banana strawberry'; echo '<hr>'; preg_match('/ (?: (?! ### Stop words go here: straw ) .)* /x', $str, $matches); print_r($matches); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Rowno Posted August 6, 2008 Author Share Posted August 6, 2008 This doesn't exactly work because went I put the regex in a preg_replace, it only returned 's' not 'straw'. Quote Link to comment Share on other sites More sharing options...
effigy Posted August 6, 2008 Share Posted August 6, 2008 <pre> <?php echo $str = 'apple orange banana strawberry kiwi grapefruit cherry grape'; echo '<hr>'; $keeps = array( 'range', 'straw', 'grape', 'orangutan', ); $pattern = '/(' . join('|', $keeps) . ')/'; $pieces = preg_split($pattern, $str, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY); print_r(preg_grep($pattern, $pieces)); ?> </pre> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.