Reputation: 1185
I need to remove all occurrences of a --word prefixed by two dashes from a string in PHP.
I gather that I do so via preg_replace()
, but I can't write a Regex expression to do it.
Upvotes: 0
Views: 64
Reputation: 89547
Like this:
$result = preg_replace ('~--tHeUgLyWoRd\b~', '', $mystring);
for any words (Hippolyte example):
$result = preg_replace('~--\w++\b~', '', $mystring);
Words can contains hyphens:
$result = preg_replace('~--(\w++-?)++\b~', '', $mystring);
But don't have underscores:
$result = preg_replace('~--([^\W_]++-?)++\b~', '', $mystring);
Upvotes: 3