php - Matching x words in brackets regex -
i trying remove brackets string if contains 4 or more words. have been scratching head , cannot anywhere it.
preg_replace('#\([word]{4,}\)#', '', $str); # pseudo code
sample string:
robert alner fund standard open nh flat race (supported andrew stewart charitable foundation)
to match (more x words in brackets) , remove:
(supported andrew stewart charitable foundation)
i have 2 sources of data, , using:
similar_text($str1, $str2, &$percent)
to compare , longish strings in brackets unique 1 source.
well, you're close...
preg_replace('#\((\b\w+\b[^\w)]*){4,}\)#', '', $str);
basically, inner sub-pattern (\b\w+\b[^\w)]*)
matches word-boundary (meaning not in-between 2 word characters) followed @ least 1 word character (a-z0-9), followed word-boundary, , followed 0 or more characters not word characters , not )
...
testing with:
$tests = array( 'test1 (this three)', 'test2 (this 4 words)', 'test3 (this 4 words) , (this three)', 'test4 (this 5 words inside)', ); foreach ($tests $str) { echo $str . " - " . preg_replace('#\((\b\w+\b[^\w)]*){4,}\)#', '', $str) . "\n"; }
gives:
test1 (this three) - test1 (this three) test2 (this 4 words) - test2 test3 (this 4 words) , (this three) - test3 , (this three) test4 (this 5 words inside) - test4
Comments
Post a Comment