Jump to content

[SOLVED] Exploding array


Thethug

Recommended Posts

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);
?>

Link to comment
Share on other sites

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); 
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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);
}

Link to comment
Share on other sites

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








?>

Link to comment
Share on other sites

<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>';
}
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
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.