Skip to content Skip to sidebar Skip to footer

Php Regex: Modify To Only Allow One Word

I'm using the contact form 7 plugin for wordpress in combination with contact form db to display the field results in the front end. I'm trying to filter out the results in the sho

Solution 1:

Here's a regex that won't non-words (including punctuation). It allows unicode though:

/^\w+$/u

DEMO

Pass:

  • correct
  • foobar
  • definitelynot
  • unicodeæøå

No pass:

  • foo bar
  • bar-foo
  • foo.bar
  • noway, sir

Solution 2:

How about this one:

/^(\S+)/

This capture all characters that are not spaces from the beginning of the string.

Solution 3:

Try ^s[a-zA-Z0-9]\*$ Start with s and followed by any number of characters inside brackets []. Other way would be ^s[a-zA-Z0-9]\*\S$ which insists whitespace to be in the end of word! I did not test this code, but idea should be there.

Post a Comment for "Php Regex: Modify To Only Allow One Word"