peter.o
peter.o

Reputation: 3530

Exclude whole word using regexp

I'd like to exclude word user and gallery using regular expression.

^/(?!user|gallery)([a-z0-9_-]{3,64})$

My regexp also excludes words like mygallery (in which gallery is a substring). I want to have mygallery included.

Thanks a lot in advance.

Upvotes: 0

Views: 119

Answers (2)

Prasanth Bendra
Prasanth Bendra

Reputation: 32730

Try this :

$str = "mygallery and gallery user and username";
echo preg_replace("/\bgallery\b|\buser\b/","",$str);

Output :

mygallery and and username

Upvotes: 2

Tim Pietzcker
Tim Pietzcker

Reputation: 336108

Your regex should already accept mygallery, but it would reject username. Add anchors to make sure it doesn't:

^/(?!user$|gallery$)([a-z0-9_-]{3,64})$

Upvotes: 2

Related Questions