Jump to content

[SOLVED] Finding the longest word in a string


Yesideez

Recommended Posts

Is there a php function to find the longest word in a word list or sentence?

 

I've got a massive list so if there isn't a function how would be the most efficient way of doing it?

 

I've looked at playing with arrays but can't seem to find a quick and easy way...

<?php
function longest($str) {
  $ar = explode(" ", $str);
  $ln = $ar[0];
  for($i=0; $i < count($ar); $i++) {
    if (strlen($ar[$i]) > strlen($ln)) {
      $ln = $ar[$i];
    }
  }
  return $ln;
}
?>

 

Arrays ARE quick and easy.

<?php
$wordList = file('wordlist.txt'); // put them into an array splitting at \n (newline)

$long = array("len" => 0, "word" => "");
foreach ($wordList as $word) {
         $curr = strlen($word);
         if ($curr > $long) { 
             $long['len'] = $curr;
             $ong['word'] = $word;
         }
}

print_r($long);
?>

I've tried the foreach method and I get nothing returned, just an empty array although I'm not sure if I'm doing this right. I've never used foreach before or created arrays using keys, only indices. Some sample data is:

A DAM ARK ZERO NOR OR
A DAM ARK RE NOR ORZO
A DAM ARK ERR ON ORZO
A DAM ARK ERR NO ORZO
A DAM ARK ERR NOR ZOO
A DAM RAZOR KRONE OR
A DAM RAZOR ERR NOOK
A DAMN OKRA ERR ORZO
A DAMN ARK ERROR ZOO
A DAMN ARK RE ORZO OR

Basically I have a massive list like that. What I'm wanting to do is remove any duplicates and return the top 10 longest words. My code as it stands now is this:

<?php
    $wordList=explode("\n",str_replace("\n"," ",$txtwordlist));
    $long=array("len" => 0,"word" => "");
    foreach ($wordList as $word) {
      $curr=strlen($word);
      if ($curr>$long) { 
        $long['len']=$curr;
        $ong['word']=$word;
      }
    }
?>

if you are removing duplicates, their won't be much to put in your top 10

 

A DAM ARK ZERO NOR OR
A DAM ARK RE NOR ORZO
A DAM ARK ERR ON ORZO
A DAM ARK ERR NO ORZO
A DAM ARK ERR NOR ZOO
A DAM RAZOR KRONE OR
A DAM RAZOR ERR NOOK
A DAMN OKRA ERR ORZO
A DAMN ARK ERROR ZOO
A DAMN ARK RE ORZO OR

 

would become

 

ZERO
ON
NO
KRONE
NOOK
OKRA
ERROR

 

then for your longest words

 

KRONE // longest 5 (assuming KRONE and ERROR is found only once)
ERROR // same

 

the php code used:

 

   $wordList=explode("\n",str_replace("\n"," ",$txtwordlist));
   $long=array("len" => 0,"word" => "");
   foreach ($wordList as $word) {
     $curr=strlen($word);
     // this will give you only the longest word the complete string, 
     // use $long['len'][] to get all longest words
     if ($curr>$long) {
       $long['len']=$curr;
       $ong['word']=$word;
     }
   }

if you are removing duplicates, their won't be much to put in your top 10

 

A DAM ARK ZERO NOR OR
A DAM ARK RE NOR ORZO
A DAM ARK ERR ON ORZO
A DAM ARK ERR NO ORZO
A DAM ARK ERR NOR ZOO
A DAM RAZOR KRONE OR
A DAM RAZOR ERR NOOK
A DAMN OKRA ERR ORZO
A DAMN ARK ERROR ZOO
A DAMN ARK RE ORZO OR

That is just sample data. My actual list is a few hundred lines long maybe even a couple thousand!

<?php
$wordList = file('wordlist.txt'); // put them into an array splitting at \n (newline)

$long = array("len" => 0, "word" => "");
foreach ($wordList as $word) {
         $curr = strlen($word);
         if ($curr > $long['len']) { 
             $long['len'] = $curr;
             $ong['word'] = $word;
         }
}

print_r($long);
?>

 

Sorry minor mistake on the is statement, this should be good.

if you are removing duplicates, their won't be much to put in your top 10

 

A DAM ARK ZERO NOR OR
A DAM ARK RE NOR ORZO
A DAM ARK ERR ON ORZO
A DAM ARK ERR NO ORZO
A DAM ARK ERR NOR ZOO
A DAM RAZOR KRONE OR
A DAM RAZOR ERR NOOK
A DAMN OKRA ERR ORZO
A DAMN ARK ERROR ZOO
A DAMN ARK RE ORZO OR

 

would become

 

ZERO
ON
NO
KRONE
NOOK
OKRA
ERROR

 

then for your longest words

 

KRONE // longest 5 (assuming KRONE and ERROR is found only once)
ERROR // same

 

the php code used:

 

    $wordList=explode("\n",str_replace("\n"," ",$txtwordlist));
    $long=array("len" => 0,"word" => "");
    foreach ($wordList as $word) {
      $curr=strlen($word);
      // this will give you only the longest word the complete string, 
      // use $long['len'][] to get all longest words
      if ($curr>$long) {
        $long['len']=$curr;
        $ong['word']=$word;
      }
    }

As I said earlier - that was SAMPLE data!!!

 

I've finished my script and am running data through it with around 65,000 lines of words. Once I've removed all the duplicates I'm still left with between 500 and 1,000 words to play with.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.