php_joe Posted June 14, 2006 Share Posted June 14, 2006 Hi.I have a script that will list each word entered into a textbox. What I am having trouble figuring out is how to remove the array keys. What is the best way to do this? is there a way to convert an array into a string and simply echo it?Thanks,Joe Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/ Share on other sites More sharing options...
zq29 Posted June 14, 2006 Share Posted June 14, 2006 [!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]is there a way to convert an array into a string and simply echo it?[/quote] You could use implode()[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]What I am having trouble figuring out is how to remove the array keys.[/quote] You can use unset() - unset($array[4],$array[8],$array[9]) Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45483 Share on other sites More sharing options...
kenrbnsn Posted June 14, 2006 Share Posted June 14, 2006 Your function is doing way too much work. I took the liberty of rewriting it. Take a look at this code:[code]<?phpif (!function_exists('word_count')) { function word_count($str,$n=true){ if (strstr($str,' ')) !== false) $str = str_replace(' ',' ',$str); $b = explode(' ', $str); if($n) return($b); else return(count($b)); }}if (isset($_POST['str'])) { $c = word_count($_POST['str']); // it return an array echo '<pre>' . print_r($c,true) . '</pre>';}?>[/code][list][*]I changed your second parameter to be either true or false. This simplifies the return.[*]You don't need to loop through the string to get rid of multiple spaces, [a href=\"http://www.php.net/str_replace\" target=\"_blank\"]str_replace()[/a] will do that in one call[*]You don't have to loop through the array to count the entries, the [a href=\"http://www.php.net/count\" target=\"_blank\"]count()[/a] function does that.[/list]Ken Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45495 Share on other sites More sharing options...
php_joe Posted June 14, 2006 Author Share Posted June 14, 2006 Thanks [b]SemiApocalyptic[/b].I am trying to figure out implode() right now.I should have been more precise in my OP. I want to take a block of text (entered via a form) and list each word that was entered, but with only one example of each word (no duplicates).I thought that the code that I had did that (but in an Array) but I was wrong. It just listed each word in the text. :(Anyone have any suggestions for me?Thanks,Joe[!--quoteo(post=383758:date=Jun 14 2006, 09:27 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ Jun 14 2006, 09:27 PM) [snapback]383758[/snapback][/div][div class=\'quotemain\'][!--quotec--]Your function is doing way too much work. I took the liberty of rewriting it. Ken[/quote]Thanks Ken!You did quite a bit of good work.Joe [img src=\"style_emoticons/[#EMO_DIR#]/smile.gif\" style=\"vertical-align:middle\" emoid=\":smile:\" border=\"0\" alt=\"smile.gif\" /] Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45496 Share on other sites More sharing options...
zq29 Posted June 14, 2006 Share Posted June 14, 2006 Ok, you could take the contents of your form input, and run it through explode(" ",$input); which will create an array, each key holding one word. You can then run this array through array_unique() to remove any duplicate values, leaving an array containing only unique words. If you need it back in a string, you can run the array through implode(" ",$array), or to count the unique words, run the array through count().Oh, if you needed each unique word on a new line, you could just run the array through a foreach() statement. Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45500 Share on other sites More sharing options...
kenrbnsn Posted June 14, 2006 Share Posted June 14, 2006 Another way of echoing the array with each element on a new line would be:[code]<?php echo implode("<br />\n",$yourarray)."<br />\n"; ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45558 Share on other sites More sharing options...
php_joe Posted June 14, 2006 Author Share Posted June 14, 2006 Thanks a lot [b]SemiApocalyptic[/b] and [b]kenrbnsn[/b]!You guys are the greatest!I hope that I will be good enough to contribute as well as benifit.JoeI finished the code so that it does what I wanted it to do:[code]<html><body><form method="post"><textarea col="50" rows="10" name="str"></textarea><input type="submit" value="Submit"></form><?$str = str_replace(",", "", $str);$str = str_replace(".", "", $str);$str = str_replace(";", "", $str);$str = str_replace(";", "", $str);$str = str_replace("'", "", $str);$str = str_replace('"', '', $str);$str = str_replace("?", "", $str);$str = str_replace("!", "", $str);$array = explode(" ",$str);$list = array_unique($array);$print = implode(" ",$list);echo "$print";?></body></html>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45587 Share on other sites More sharing options...
kenrbnsn Posted June 14, 2006 Share Posted June 14, 2006 These lines can be reduced to one:[code]<?php$str = str_replace(",", "", $str);$str = str_replace(".", "", $str);$str = str_replace(";", "", $str);$str = str_replace(";", "", $str);$str = str_replace("'", "", $str);$str = str_replace('"', '', $str);$str = str_replace("?", "", $str);$str = str_replace("!", "", $str);?>[/code]to[code]<?php $str = str_replace(array(',','.',';',"'",'"','?','!'),'',$str); ?>[/code]Ken Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45592 Share on other sites More sharing options...
poirot Posted June 14, 2006 Share Posted June 14, 2006 Or a regular expression:[code]$str = preg_replace("/\W/", "", $str);[/code]And there is also a built-in function called str_word_count[a href=\"http://www.php.net/str_word_count\" target=\"_blank\"]http://www.php.net/str_word_count[/a] Quote Link to comment https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45649 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.