将 PHP 中的文本输入限制为仅允许五 (5) 个单词

发布于 2024-09-28 02:12:35 字数 88 浏览 4 评论 0原文

我想知道如何使用 PHP 在文本输入中只允许五 (5) 个单词。

我知道我可以使用 strlen 函数进行字符计数,但我想知道如何对单词进行计数。

I want to know how I can allow only five (5) words on text input using PHP.

I know that I can use the strlen function for character count, but I was wondering how I can do it for words.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

月隐月明月朦胧 2024-10-05 02:12:35

你可以这样尝试:

$string = "this has way more than 5 words so we want to deny it ";

//edit: make sure only one space separates words if we want to get really robust: 
//(found this regex through a search and havent tested it)
$string  = preg_replace("/\\s+/", " ", $string);

//trim off beginning and end spaces;
$string = trim($string);

//get an array of the words
$wordArray = explode(" ", $string);

//get the word count
$wordCount = sizeof($wordArray);

//see if its too big
if($wordCount > 5) echo "Please make a shorter string";

应该可以:-)

You can try it like this:

$string = "this has way more than 5 words so we want to deny it ";

//edit: make sure only one space separates words if we want to get really robust: 
//(found this regex through a search and havent tested it)
$string  = preg_replace("/\\s+/", " ", $string);

//trim off beginning and end spaces;
$string = trim($string);

//get an array of the words
$wordArray = explode(" ", $string);

//get the word count
$wordCount = sizeof($wordArray);

//see if its too big
if($wordCount > 5) echo "Please make a shorter string";

should work :-)

爱*していゐ 2024-10-05 02:12:35

如果你这样做;

substr_count($_POST['your text box'], ' ');

并将其限制为 4

If you do;

substr_count($_POST['your text box'], ' ');

And limit it to 4

绳情 2024-10-05 02:12:35

如果 $input 是您的输入字符串,

$wordArray = explode(' ', $input);
if (count($wordArray) > 5)
  //do something; too many words

尽管我真的不知道您为什么要使用 php 进行输入验证。如果您只使用 javascript,则可以在表单提交之前让用户有机会更正输入。

If $input is your input string,

$wordArray = explode(' ', $input);
if (count($wordArray) > 5)
  //do something; too many words

Although I honestly don't know why you'd want to do your input validation with php. If you just use javascript, you can give the user a chance to correct the input before the form submits.

无远思近则忧 2024-10-05 02:12:35

除了使用explode() 或substr_count() 的所有这些不错的解决方案之外,为什么不简单地使用PHP 的内置函数来计算字符串中的单词数。我知道函数名称对于此目的来说并不是特别直观,但是:

$wordCount = str_word_count($string);

将是我的建议。

请注意,当使用多字节字符集时,这不一定很有效。 类似以下内容:

define("WORD_COUNT_MASK", "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u");

function str_word_count_utf8($str)
{
    return preg_match_all(WORD_COUNT_MASK, $str, $matches);
} 

在这种情况下,在 str_word_count() 上建议使用 手册页

Aside from all these nice solutions using explode() or substr_count(), why not simply use PHP's built-in function to count the number of words in a string. I know the function name isn't particularly intuitive for this purpose, but:

$wordCount = str_word_count($string);

would be my suggestion.

Note, this isn't necessarily quite as effective when using multibyte character sets. In that case, something like:

define("WORD_COUNT_MASK", "/\p{L}[\p{L}\p{Mn}\p{Pd}'\x{2019}]*/u");

function str_word_count_utf8($str)
{
    return preg_match_all(WORD_COUNT_MASK, $str, $matches);
} 

is suggested on the str_word_count() manual page

无声情话 2024-10-05 02:12:35

您必须执行两次,一次在客户端使用 JavaScript,然后在服务器端使用 PHP。

You will have to do it twice, once using JavaScript at the client-side and then using PHP at the server-side.

不打扰别人 2024-10-05 02:12:35

在PHP中,使用split函数将其按空格分割。这样你就会得到一个数组中的单词。然后检查数组的长度。

$mytextboxcontent=$_GET["txtContent"];

$words = explode(" ", $mytextboxcontent);
$numberOfWords=count($words);

 if($numberOfWords>5)
 {
   echo "Only 5 words allowed";
 }
 else
 {
  //do whatever you want....
 }

我没有测试这个。希望这有效。我的机器上现在没有设置PHP环境。

In PHP, use split function to split it by space.So you will get the words in an array. Then check the length of the array.

$mytextboxcontent=$_GET["txtContent"];

$words = explode(" ", $mytextboxcontent);
$numberOfWords=count($words);

 if($numberOfWords>5)
 {
   echo "Only 5 words allowed";
 }
 else
 {
  //do whatever you want....
 }

I didn't test this.Hope this works. I don't have a PHP environment set up on my machine now.

静若繁花 2024-10-05 02:12:35

你可以数一下空格的数量...

$wordCount = substr_count($input, ' ');

You could count the number of spaces...

$wordCount = substr_count($input, ' ');
‖放下 2024-10-05 02:12:35

我想你想先用Javascript来做这件事,只允许用户插入5个单词(然后用PHP验证它以避免恶意)

在JS中,你需要在输入字符时计算字符数并保留单词数您编写(通过增加每个空格

的计数器)看一下以下代码: http://javascript.internet.com/forms/limit-characters-and-words-entered.html

i think you want to do it first with Javascript to only allow the user to insert 5 words (and after validate it with PHP to avoid bad intentions)

In JS you need to count the chars as you type them and keep the count of the words you write ( by incrementing the counter each space)

Take a look of this code: http://javascript.internet.com/forms/limit-characters-and-words-entered.html

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文