Php 字符串中最长的单词

Php 字符串中最长的单词,php,string,Php,String,如何获取字符串中最长的单词 例如 要返回“大象”循环字符串中的单词,跟踪到目前为止最长的单词,请执行以下操作: <?php $string = "Where did the big Elephant go?"; $words = explode(' ', $string); $longestWordLength = 0; $longestWord = ''; foreach ($words as $word) { if (strlen($word) > $longestW

如何获取字符串中最长的单词

例如


要返回“大象”

循环字符串中的单词,跟踪到目前为止最长的单词,请执行以下操作:

<?php
$string = "Where did the big Elephant go?";
$words  = explode(' ', $string);

$longestWordLength = 0;
$longestWord = '';

foreach ($words as $word) {
   if (strlen($word) > $longestWordLength) {
      $longestWordLength = strlen($word);
      $longestWord = $word;
   }
}

echo $longestWord;
// Outputs: "Elephant"
?>

可以提高一点效率,但你明白了。

这个方法怎么样--按空格分割,然后按字符串长度排序,然后抓住第一个:

<?php

$string = "Where did the big Elephant go?";

$words = explode(' ', $string);

usort($words, function($a, $b) {
    return strlen($b) - strlen($a);
});

$longest = $words[0];

echo $longest;

下面是另一个解决方案:

$array = explode(" ",$string);
$result = "";
foreach($array as $candidate)
{
if(strlen($candidate) > strlen($result))
$result = $candidate
}
return $result;

更新:这里是另一个更短的方法(这是一个全新的;):


与已发布的内容类似,但使用
str\u word\u count
提取单词(通过在空格处拆分,标点符号也将被计数):


在处理文本时,这是一个非常有用的函数,因此最好为此创建一个PHP函数:

function longestWord($txt) {
    $words = preg_split('#[^a-z0-9áéíóúñç]#i', $txt, -1, PREG_SPLIT_NO_EMPTY);
    usort($words, function($a, $b) { return strlen($b) - strlen($a); });
    return $words[0];
}
echo longestWord("Where did the big Elephant go?");
// prints Elephant

在此处测试此函数:

一种可能的解决方案是在常用分隔符(如空格)上拆分句子,然后迭代每个单词,只保留对最大单词的引用

请注意,这将找到第一个最大的单词

<?php
function getLargestWord($str) {
  $strArr = explode(' ', $str); // Split the sentence into an word array
  $lrgWrd = ''; // initial value for comparison
  for ($i = 0; $i < count($strArr); $i++) {
    if (strlen($strArr[$i]) > strlen($lrgWrd)) { // Word is larger
      $lrgWrd = $strArr[$i]; // Update the reference
    }
  }
  return $lrgWrd;
}
// Example:
echo getLargestWord('Where did the big Elephant go?');
?>


到目前为止,您尝试了什么?虽然有人可能会给你答案,但如果你先尝试一些东西,你会学到更多。(而且,如果你表现出你已经考虑过了,你就更有可能得到更好的答案。)你在15秒内就写下了@JMCCreative:我真是太棒了。@Tomalak你的威严应该与
$longestWord
相呼应,而不是
$word
;)@弗兰克沃科:你也应该知道这里的“字长”包括任何标点符号。可以使用PHP中的相关字符串函数去除标点符号。我会让你在闲暇时做一些研究。@mvds:好吧,我放弃。这是一个最长的令牌查找示例。@Tomalak实际上,它的速度稍微慢一点。尽管如此,它还是一个替代方案,甚至可能是更漂亮的解决方案。+1用于使用
str\u word\u count()
。不过,为了简洁起见,我会将该函数立即放在对
usort()的调用中。我是jQuery约定的奴隶!但我不喜欢创建函数(并污染符号表)的需要
create_function
可以解决这个问题,但是事情非常繁琐。@Tomalak Geret'kal:我也不喜欢它,但有时它是不可避免的(这里并不是说这种情况)。@Felix我不确定您没有数组引用是什么意思。我建议使用usort($words,function($a,$b){/*code*/})应该可以正常工作。这就是@lonesomeday采用的方法。@Jonathan Sampson:啊,我以为你的意思是将函数
str\u word\u count
放入
usort
:D我想直接传递函数,但我仍然倾向于以5.2的方式给出答案。它更一般,了解PHP5.3的人应该能够对其进行转换…虽然提供的代码回答了问题,但您可以假设阅读问题的人不会理解它,请提供注释解释您正在做什么,因为这是一个更简单的问题。
function reduce($v, $p) {
    return strlen($v) > strlen($p) ? $v : $p;
}

echo array_reduce(str_word_count($string, 1), 'reduce'); // prints Elephant
$string = "Where did the big Elephant go?";

$words = str_word_count($string, 1);

function cmp($a, $b) {
    return strlen($b) - strlen($a);
}

usort($words, 'cmp');

print_r(array_shift($words)); // prints Elephant
function longestWord($txt) {
    $words = preg_split('#[^a-z0-9áéíóúñç]#i', $txt, -1, PREG_SPLIT_NO_EMPTY);
    usort($words, function($a, $b) { return strlen($b) - strlen($a); });
    return $words[0];
}
echo longestWord("Where did the big Elephant go?");
// prints Elephant
<?php
function getLargestWord($str) {
  $strArr = explode(' ', $str); // Split the sentence into an word array
  $lrgWrd = ''; // initial value for comparison
  for ($i = 0; $i < count($strArr); $i++) {
    if (strlen($strArr[$i]) > strlen($lrgWrd)) { // Word is larger
      $lrgWrd = $strArr[$i]; // Update the reference
    }
  }
  return $lrgWrd;
}
// Example:
echo getLargestWord('Where did the big Elephant go?');
?>