在PHP中,如何确定字符串中的某个单词是否大于50个字符?

在PHP中,如何确定字符串中的某个单词是否大于50个字符?,php,Php,如何在PHP中做这样的事情?我喜欢这个论坛上唯一的C#解决方案 例如,我有一个字符串: $string_to_check = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa rrrr fe we we hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhererererereerdfsdfsdfsdfsdfsdfsdfsdfsdfsdfttttfsd hhghhh

如何在PHP中做这样的事情?我喜欢这个论坛上唯一的C#解决方案

例如,我有一个字符串:

$string_to_check = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa rrrr fe we we hhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhererererereerdfsdfsdfsdfsdfsdfsdfsdfsdfsdfttttfsd hhghhhhhhhhhhhhhhhhhh fd s hoefjsd k
bla bla bla";
我想做一个if条件,当字符串包含一个50个或更多字符长的单词时,返回false;否则返回true

如有任何关于如何解决此问题的建议,我们将不胜感激。

尝试此功能:

function not_long_word($sentence, $length = 50) {
    $words = explode(' ', $string);
    foreach ($words as $key => $value) {
      if (strlen($value) > $length) return false;
    }
    return true;
}
用法:

$text = "word wooooooooooooooooooooooooooooooooooooooooooooooooooooooooooord";
if (not_long_word($text)) {
    echo "there no word longer than 50!";
}
尝试此功能:

function not_long_word($sentence, $length = 50) {
    $words = explode(' ', $string);
    foreach ($words as $key => $value) {
      if (strlen($value) > $length) return false;
    }
    return true;
}
用法:

$text = "word wooooooooooooooooooooooooooooooooooooooooooooooooooooooooooord";
if (not_long_word($text)) {
    echo "there no word longer than 50!";
}

应该是这样的:

if(strlen($string_to_check) < 50 )
{
 ...
}
else {
...
}
if(strlen($string\u to\u check)<50)
{
...
}
否则{
...
}

应该是这样的:

if(strlen($string_to_check) < 50 )
{
 ...
}
else {
...
}
if(strlen($string\u to\u check)<50)
{
...
}
否则{
...
}

首先,将其拆分为单个单词(假设空格是分隔符),然后确定是否有任何单词长度超过50个字符:

$array = explode(" ",$string);
foreach ($array as $word) { 
  if (strlen($word) > 50) {
    echo "{$word}\n"
  }
}
如果分隔符可能有多个空格/制表符,则可以选择正则表达式:

$array = preg_split('[\t\s]+', $string);

首先,将其拆分为单个单词(假设空格是分隔符),然后确定是否有任何单词长度超过50个字符:

$array = explode(" ",$string);
foreach ($array as $word) { 
  if (strlen($word) > 50) {
    echo "{$word}\n"
  }
}
如果分隔符可能有多个空格/制表符,则可以选择正则表达式:

$array = preg_split('[\t\s]+', $string);

经过测试,效果良好

check_word_length( $string_to_check );

function check_word_length( $string_to_check ){
    foreach ( explode(' ', $string_to_check )  as $word) {
        if ( strlen($word) > 50 ) return false;
    }
    return true;
}

经过测试,效果良好

check_word_length( $string_to_check );

function check_word_length( $string_to_check ){
    foreach ( explode(' ', $string_to_check )  as $word) {
        if ( strlen($word) > 50 ) return false;
    }
    return true;
}

OP要检查单个单词,而不是完整的句子。OP要检查单个单词,而不是完整的句子。