Jump to content


Photo

Word List


  • Please log in to reply
8 replies to this topic

#1 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 14 June 2006 - 12:47 PM

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


[url=http://darkswordsguide.com][/url]

#2 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 14 June 2006 - 01:00 PM

[!--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])

#3 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 June 2006 - 01:27 PM

Your function is doing way too much work. I took the liberty of rewriting it. Take a look at this code:
<?php
if (!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>';
}?>
  • 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.
Ken


#4 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 14 June 2006 - 01:31 PM

Thanks SemiApocalyptic.

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) View Post[/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\" /]
[url=http://darkswordsguide.com][/url]

#5 Kris

Kris
  • Staff Alumni
  • Advanced Member
  • 2,755 posts
  • LocationThe Internet

Posted 14 June 2006 - 01:39 PM

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.

#6 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 June 2006 - 02:39 PM

Another way of echoing the array with each element on a new line would be:
<?php echo implode("<br />\n",$yourarray)."<br />\n"; ?>

Ken

#7 php_joe

php_joe
  • Members
  • PipPipPip
  • Advanced Member
  • 175 posts

Posted 14 June 2006 - 03:24 PM

Thanks a lot SemiApocalyptic and kenrbnsn!

You guys are the greatest!

I hope that I will be good enough to contribute as well as benifit.

Joe

I finished the code so that it does what I wanted it to do:

<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>

[url=http://darkswordsguide.com][/url]

#8 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 14 June 2006 - 03:41 PM

These lines can be reduced to one:
<?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);
?>
to
<?php $str = str_replace(array(',','.',';',"'",'"','?','!'),'',$str); ?>

Ken

#9 poirot

poirot
  • Members
  • PipPipPip
  • Advanced Member
  • 646 posts
  • LocationAustin, TX

Posted 14 June 2006 - 05:48 PM

Or a regular expression:

$str = preg_replace("/\W/", "", $str);

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]
~ D Kuang




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users