简体   繁体   中英

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 shortcode, eg

<?php echo do_shortcode('[cfdb-value form="Testing" filter="FirstField~~/^s/"]'); ?>

This filter will only show values of FirstField that start with the letter s , is it possible to adapt this code to only show one word values (ie words with no spaces in). If this is at all possible? Any suggestions would be greatly appreciated!

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.

How about this one:

/^(\S+)/

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM