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? Link to comment https://forums.phpfreaks.com/topic/117698-matching-everything-except-some-selected-words/ 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> Link to comment https://forums.phpfreaks.com/topic/117698-matching-everything-except-some-selected-words/#findComment-607534 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? Link to comment https://forums.phpfreaks.com/topic/117698-matching-everything-except-some-selected-words/#findComment-608220 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> Link to comment https://forums.phpfreaks.com/topic/117698-matching-everything-except-some-selected-words/#findComment-608603 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'. Link to comment https://forums.phpfreaks.com/topic/117698-matching-everything-except-some-selected-words/#findComment-609322 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> Link to comment https://forums.phpfreaks.com/topic/117698-matching-everything-except-some-selected-words/#findComment-609732 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.