php regex: modify to only allow one word

那年仲夏 提交于 2021-01-29 10:54:48

问题


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, e.g.

<?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 (i.e. words with no spaces in). If this is at all possible? Any suggestions would be greatly appreciated!


回答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



回答2:


How about this one:

/^(\S+)/

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




回答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.



来源:https://stackoverflow.com/questions/20701035/php-regex-modify-to-only-allow-one-word

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!