You can do it like this:
$result = preg_replace('~(?:\s*+\S*pu(?:ll|sh)\S*)+~', '', $str);
pattern details:
~ # pattern delimiter
(?: # open a non capturing group
\s*+ # zero or more whitespaces (possessive)
\S* # zero or more non-whitespaces
pu(?:ll|sh) # push or pull
\S*
)+ # repeat the non capturing group (1 or more)
~
Note: if the string begins with "push" or "pull", this pattern may let a leading whitespace, in this case, use rtrim
to remove it.
Depending how looks your string, this variant that unrools the loop (?:[^p\s]+|p+(?!u(?:ll|sh)))*+
(that replaces more explicitly \S*
) may be more performant:
(?:\s*+[^p\s]*+(?:p+(?!u(?:ll|sh))[^p\s]*)*+pu(?:ll|sh)\S*)+
about possessive quantifiers and lookarounds