Jump to content

Recommended Posts

[!--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])
Link to comment
https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45483
Share on other sites

Your function is doing way too much work. I took the liberty of rewriting it. Take a look at this code:
[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>';
}?>[/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
Link to comment
https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45495
Share on other sites

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\" /]
Link to comment
https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45496
Share on other sites

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.
Link to comment
https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45500
Share on other sites

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.

Joe

I 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]
Link to comment
https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45587
Share on other sites

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
Link to comment
https://forums.phpfreaks.com/topic/11974-word-list/#findComment-45592
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.