Yesideez Posted March 18, 2007 Share Posted March 18, 2007 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... Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/ Share on other sites More sharing options...
chawezul Posted March 18, 2007 Share Posted March 18, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-209960 Share on other sites More sharing options...
per1os Posted March 18, 2007 Share Posted March 18, 2007 <?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); ?> Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-209962 Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 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; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-209985 Share on other sites More sharing options...
ignace Posted March 18, 2007 Share Posted March 18, 2007 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; } } Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-209990 Share on other sites More sharing options...
Yesideez Posted March 18, 2007 Author Share Posted March 18, 2007 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! Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-209991 Share on other sites More sharing options...
per1os Posted March 18, 2007 Share Posted March 18, 2007 <?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. Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-210168 Share on other sites More sharing options...
Yesideez Posted March 19, 2007 Author Share Posted March 19, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/43244-solved-finding-the-longest-word-in-a-string/#findComment-210246 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.