Kaelan Fouwels
Kaelan Fouwels

Reputation: 1185

Remove all --arguments from string

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

Answers (1)

Casimir et Hippolyte
Casimir et Hippolyte

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

Related Questions