3v4l.org

run code in 200+ php & hhvm versions
Bugs & Features
<?php $meta_pattern = '/(?<=^|%3$s|\s)(?:(?!\S*%3$s\S*)\S+\s+){0,%2$d}(?:(?!\S*%3$s\S*%1$s)\S*)?%1$s\S*?(?:\s+\S+?){0,%2$d}(?=$|%3$s|\s)/'; $reg = sprintf($meta_pattern, preg_quote('search','/'), 3, preg_quote('$@','/')); $tests = array( array('input' => 'a b c d search e f g h', 'expected' => 'b c d search e f g'), array('input' => 'b c d search e f g h', 'expected' => 'b c d search e f g'), array('input' => 'a b c d search e f g', 'expected' => 'b c d search e f g'), array('input' => 'c d search e f g h', 'expected' => 'c d search e f g'), array('input' => 'a b c d search e f', 'expected' => 'b c d search e f'), array('input' => 'a$@b c d search e f g h', 'expected' => 'b c d search e f g'), array('input' => 'a b c d search e f g$@h', 'expected' => 'b c d search e f g'), array('input' => 'a b$@c d search e f g h', 'expected' => 'c d search e f g'), array('input' => 'a b c d search e f$@g h', 'expected' => 'b c d search e f'), array('input' => 'a b c$@d search e f g h', 'expected' => 'd search e f g'), array('input' => 'a b c d search e$@f g h', 'expected' => 'b c d search e'), array('input' => 'a b c d$@search e f g h', 'expected' => 'search e f g'), array('input' => 'a b c d search$@e f g h', 'expected' => 'b c d search'), array('input' => 'a b c d$@search$@e f g h', 'expected' => 'search'), array('input' => 'a b c d xsearchx e f g h', 'expected' => 'b c d xsearchx e f g'), array('input' => 'b c d xsearchx e f g h', 'expected' => 'b c d xsearchx e f g'), array('input' => 'a b c d xsearchx e f g', 'expected' => 'b c d xsearchx e f g'), array('input' => 'c d xsearchx e f g h', 'expected' => 'c d xsearchx e f g'), array('input' => 'a b c d xsearchx e f', 'expected' => 'b c d xsearchx e f'), array('input' => 'a$@b c d xsearchx e f g h', 'expected' => 'b c d xsearchx e f g'), array('input' => 'a b c d xsearchx e f g$@h', 'expected' => 'b c d xsearchx e f g'), array('input' => 'a b$@c d xsearchx e f g h', 'expected' => 'c d xsearchx e f g'), array('input' => 'a b c d xsearchx e f$@g h', 'expected' => 'b c d xsearchx e f'), array('input' => 'a b c$@d xsearchx e f g h', 'expected' => 'd xsearchx e f g'), array('input' => 'a b c d xsearchx e$@f g h', 'expected' => 'b c d xsearchx e'), array('input' => 'a b c d$@xsearchx e f g h', 'expected' => 'xsearchx e f g'), array('input' => 'a b c d xsearchx$@e f g h', 'expected' => 'b c d xsearchx'), array('input' => 'a b c d$@xsearchx$@e f g h', 'expected' => 'xsearchx') ); foreach($tests as $test) { preg_match($reg,$test['input'],$res); if(!($res && !empty($res[0]) && $res[0]==$test['expected'])) { var_dump('Test '.$test['input'].' failed.'); } }
based on JuRYB
Output for 4.3.0 - 7.2.0