Thethug Posted October 28, 2009 Share Posted October 28, 2009 My objective is to allow the user to input a sentence, explode it by each space, and count how many words are in the sentence. I then have to output the number of words in the sentence. Here's what I have so far, but I'm not sure what to do from there. Any ideas would be appreciated. <form method='post' action=''> <input type="text" name="Department1" value = "<?=$_POST["sent"]?>" /> <input type='Submit' name='Submit' value="Submit" /> </form> <? $sentence=$_POST['sent']; $xplode = explode(",",$sentence); $count_total = count($sentence); for ($counter=0; $counter<$count_total; $counter++){ $line = each ($xplode); echo "$line[key] $line[value] <br />"; } print_r($array); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/ Share on other sites More sharing options...
Alex Posted October 28, 2009 Share Posted October 28, 2009 You're over complicating things. Do exactly what you said you wanted to do: $words = $_POST['sent']; $arr = explode(' ', $words); // Explode by a space echo count($arr); // Echo out the number. Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-945911 Share on other sites More sharing options...
Thethug Posted October 28, 2009 Author Share Posted October 28, 2009 When I tried it out, it had the textfield and submit button, but it had the number 1 on the bottom. When I typed a sentence it, it displayed nothing. Sorry about this dumb question, but I'm brand new to arrays. <form method='post' action=''> <input type="text" name="Department1" value = "<?=$_POST["sent"]?>" /> <input type='Submit' name='Submit' value="Submit" /> </form> <? $words = $_POST['sent']; $arr = explode(' ', $words); echo count($arr); ?> Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-945921 Share on other sites More sharing options...
Alex Posted October 28, 2009 Share Posted October 28, 2009 The problem is with your form, not the arrays. Are you expecting them to enter the sentence in the element named "Department1"? If so then you must change $words = $_POST['sent']; to $words = $_POST['Department1']; You don't need to put "<?=$_POST["sent"]?>" either. <form method='post' action=''> <input type="text" name="Department1" /> <input type='Submit' name='Submit' value="Submit" /> </form> <? $words = $_POST['Department1']; $arr = explode(' ', $words); echo count($arr); ?> Ultimately you should also add checking to make sure that the form was actually submitted using isset() Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-945926 Share on other sites More sharing options...
Thethug Posted October 28, 2009 Author Share Posted October 28, 2009 How would I add checking? It's displaying how many numbers there are in the sentence, which is the second part. Would adding checking help it to explode the sentence by each space? Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-945928 Share on other sites More sharing options...
Alex Posted October 28, 2009 Share Posted October 28, 2009 Well in your first post you didn't say what else you need it to you. Maybe you made a mistake? You just said it has to output the number of words in sentence. Checking would be simple, just making sure the form was submitted. if(isset($_POST['Submit'])) { $words = $_POST['Department1']; $arr = explode(' ', $words); echo count($arr); } Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-945933 Share on other sites More sharing options...
Thethug Posted October 28, 2009 Author Share Posted October 28, 2009 Also, do you (or anyone else) know how to create an array and then implode it into string where the words is separated by commas? Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946554 Share on other sites More sharing options...
Alex Posted October 28, 2009 Share Posted October 28, 2009 $array = Array('Object1', 'Object2'); echo implode(',', $array); // Object1,Object2 Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946556 Share on other sites More sharing options...
Daniel0 Posted October 28, 2009 Share Posted October 28, 2009 You're over complicating things. Do exactly what you said you wanted to do: $words = $_POST['sent']; $arr = explode(' ', $words); // Explode by a space echo count($arr); // Echo out the number. So are you See str_word_count. Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946568 Share on other sites More sharing options...
Thethug Posted October 28, 2009 Author Share Posted October 28, 2009 I wonder what stupid mistake I did this time. <form method='post' action=''> <input type="text" name="Department1" value = "<?=$_POST["Department1"]?>" /> <input type='Submit' name='Submit' value="Submit" /> </form> <? $array = $_POST['Department1']; $array = array('Object1', 'Object2'); echo implode(',', $array); // Object1,Object2 ?> Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946581 Share on other sites More sharing options...
Andy-H Posted October 28, 2009 Share Posted October 28, 2009 <form method='post' action=''> <input type="text" name="Department1" value = "<?php echo (isset($_POST['Department1'])) ? htmlentities($_POST['Department1']) : ''; ?>" /> <input type='Submit' name='Submit' value="Submit" /> </form> <?php if (isset($_POST['Submit'])) { $words = str_word_count($_POST['Department1'], 2); $n = count($words) - 1; echo '<p>' . implode(' , ', htmlentities($words, ENT_QUOTES)) . '( ' . number_format($n) . ' words )</p>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946585 Share on other sites More sharing options...
Alex Posted October 28, 2009 Share Posted October 28, 2009 You're over complicating things. Do exactly what you said you wanted to do: $words = $_POST['sent']; $arr = explode(' ', $words); // Explode by a space echo count($arr); // Echo out the number. So are you See str_word_count. Learn something new everyday. Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946586 Share on other sites More sharing options...
Thethug Posted October 28, 2009 Author Share Posted October 28, 2009 It says Warning: htmlentities() expects parameter 1 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/webalizer/implode.php on line 11 Warning: implode() [function.implode]: Invalid arguments passed in /Applications/XAMPP/xamppfiles/htdocs/webalizer/implode.php on line 11 ( 4 words ) when I try that. Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946592 Share on other sites More sharing options...
Andy-H Posted October 28, 2009 Share Posted October 28, 2009 It says Warning: htmlentities() expects parameter 1 to be string, array given in /Applications/XAMPP/xamppfiles/htdocs/webalizer/implode.php on line 11 Warning: implode() [function.implode]: Invalid arguments passed in /Applications/XAMPP/xamppfiles/htdocs/webalizer/implode.php on line 11 ( 4 words ) when I try that. <form method='post' action=''> <input type="text" name="Department1" value = "<?php echo (isset($_POST['Department1'])) ? htmlentities($_POST['Department1']) : ''; ?>" /> <input type='Submit' name='Submit' value="Submit" /> </form> <?php if (isset($_POST['Submit'])) { $words = str_word_count($_POST['Department1'], 2); $n = count($words) - 1; echo '<p>' . htmlentities(implode(' , ', $words)) . '( ' . number_format($n) . ' words )</p>'; } ?> Sorry. My bad :S Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946595 Share on other sites More sharing options...
Thethug Posted October 28, 2009 Author Share Posted October 28, 2009 Thank you Andy and Alex Quote Link to comment https://forums.phpfreaks.com/topic/179280-solved-exploding-array/#findComment-946603 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.