array_unique($words);

hi,

I have this script and i want it to read text from a form text field instead of string.
my text field is called text2

<?php
$string = 'Mary had a a little lamb, she she tied it it it to a pylon, 3000 volts whent up its ... and turned its wool to nylon';
$words = explode(' ', $string);
$duplicates = array_unique($words);
$count = array();
foreach ($words as $word) {
foreach ($duplicates as $duplicate) {
if ($word == $duplicate) {
$count[$duplicate]++;
}
}
}
foreach ($count as $k => $v) {
if ($v > 1) {
$c = $v-1; // as a count of 1 is the first occurance
echo "$k was duplicated $c times<br />\n";
}
}
?>
<form name="form1" method="post" action="">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form>
mvanthofAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

paulp75Commented:
try this
<?php
if ((isset($_POST["Submit"])))     {
$string = $_POST['text2']; 
$words = explode(' ', $string); 
$duplicates = array_unique($words); 
$count = array(); 
foreach ($words as $word) { 
foreach ($duplicates as $duplicate) { 
if ($word == $duplicate) { 
$count[$duplicate]++; 
} 
} 
} 
foreach ($count as $k => $v) { 
if ($v > 1) { 
$c = $v-1; // as a count of 1 is the first occurance 
echo "$k was duplicated $c times<br />\n"; 
} 
} 
}
?>
<form name="form1" method="post" action="">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form>

Open in new window

0
mvanthofAuthor Commented:
thanks but it will not echo other words, only 1.
if it is working, possible to split into different rows when unique words are over 20?

I will increase points to 250
0
paulp75Commented:
i'm not sure what you mean. can you show me a sample of the output you would like for say
Mary had a a little lamb, she she tied it it it to a pylon, 3000 volts whent up its ... and turned its wool to nylon

and ill see if i can help
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

mvanthofAuthor Commented:
0
paulp75Commented:
to make it only show => 3 chars change line 9 to this


if ($word == $duplicate && (strlen($word) => 3)) { 

Open in new window

0
paulp75Commented:
oops just try this

if ($word == $duplicate && (strlen($word) > 2)) {
0
mvanthofAuthor Commented:
it's not working as I wanted.... try it with this :

De tekst die tussen de De en tags staat wordt zwaarder gewaardeerd door zoekmachines. Het is verstandig om de pagina te beginnen met een tag. De tag kan gebruikt worden voor bijvoorbeeld titels of kopjes van alineas.

you see what i mean
0
mvanthofAuthor Commented:
my source now:

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
</head>
<body>
<p>
  <?php
if ((isset($_POST["Submit"])))     {
$string = $_POST['text2'];
$words = explode(' ', $string);
$duplicates = array_unique($words);
$count = array();
foreach ($words as $word) {
foreach ($duplicates as $duplicate) {
if ($word == $duplicate && (strlen($word) > 2)) {
$count[$duplicate]++;
}
}
}
foreach ($count as $k => $v) {
if ($v > 1) {
$c = $v-1; // as a count of 1 is the first occurance
echo "$k  $c x<br />\n";
}
}
}
?>
<p>- split result of counted words into 3 colomn's<br>
- display only words =&gt;3 characters</p>
<table width="700" border="1">
  <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
  </tr>
</table>

<form name="form1" method="post" action="">
  <textarea name="text2"></textarea>
  <input type="submit" name="Submit" value="Submit">
</form>
</body>
</html>
0
paulp75Commented:
give this a go, uses css instead of tables, but should work well for you
<?php
if ((isset($_POST["Submit"])))     {
$string = $_POST['text2']; 
$words = explode(' ', $string); 
$duplicates = array_unique($words); 
$count = array(); 
$recordno = 0;
echo "<div style='width:600px;'>";
foreach ($words as $word) { 
foreach ($duplicates as $duplicate) { 
if ($word == $duplicate && (strlen($word) >= 3)) { 
$count[$duplicate]++; 
 
} 
} 
} 
foreach ($count as $k => $v) { 
 
 
if ($v > 1) { 
$c = $v-1; // as a count of 1 is the first occurance 
$recordno++;
echo "<div style='width:33%; float:left;'>$recordno . $k was duplicated $c times</div>\n"; 
} 
} echo "</div";
}
?>
<form name="form1" method="post" action="" style="clear:both;">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form> 

Open in new window

0
paulp75Commented:
De tekst die tussen de De en tags staat wordt zwaarder gewaardeerd door zoekmachines. Het is verstandig om de pagina te beginnen met een tag. De tag kan gebruikt worden voor bijvoorbeeld titels of kopjes van alineas.

I can't see any words that are repeated in that text
0
mvanthofAuthor Commented:
now it's not even working...
0
ddrudikCommented:
The method below worked for me, if you have any questions or would like changes please let me know.

Note that I treat "De" and "de" as two different words (case-sensitive), I will post a variation that is case-insensitive for the counting.
<?php
if (isset($_POST["Submit"])) {
  $text2=$_POST['text2'];
  preg_match_all('/\b[a-z]+\b/i',$text2,$words);
  $words=array_unique($words[0]);
  sort($words);
  foreach ($words as $word) {
    preg_match_all('/\b'.$word.'\b/',$text2,$dup);
    $count=count($dup[0]);
    if ($count>1) {
      echo "$word was duplicated $count times<br />\n"; 
    }
  } 
}
?>
<form name="form1" method="post" action="">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form>

Open in new window

0
ddrudikCommented:
This example ignores case in the word counts:
<?php
if (isset($_POST["Submit"])) {
  $text2=$_POST['text2'];
  preg_match_all('/\b[a-z]+\b/i',$text2,$words);
  for ($i = 0; $i < count($words[0]); $i++) {
    $words[0][$i]=strtoupper($words[0][$i]);
  }
  $words=array_unique($words[0]);
  sort($words);
  foreach ($words as $word) {
    preg_match_all('/\b'.$word.'\b/i',$text2,$dup);
    $count=count($dup[0]);
    if ($count>1) {
      echo "$word was duplicated $count times<br />\n"; 
    }
  } 
}
?>
<form name="form1" method="post" action="">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form>

Open in new window

0
ddrudikCommented:
Even simpler is to use array_count_values:
<?php
if (isset($_POST["Submit"])) {
  preg_match_all('/\b[a-z]+\b/i',$_POST['text2'],$words);
  //uncomment the following 3 lines to ignore case
  //for ($i = 0; $i < count($words[0]); $i++) {
  //  $words[0][$i]=strtoupper($words[0][$i]);
  //}
  sort($words);
  $dups = array_count_values($words[0]);
  foreach ($dups as $dup=>$count) {
    if ($count>1) {
      echo "$dup was duplicated $count times<br />\n"; 
    }
  } 
}
?>
<form name="form1" method="post" action="">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form>

Open in new window

0
ddrudikCommented:
Typo in the last examples, should read:
<?php
if (isset($_POST["Submit"])) {
  preg_match_all('/\b[a-z]+\b/i',$_POST['text2'],$words);
  //uncomment the following 3 lines to ignore case
  //for ($i = 0; $i < count($words[0]); $i++) {
  //  $words[0][$i]=strtoupper($words[0][$i]);
  //}
  sort($words[0]);
  $dups = array_count_values($words[0]);
  foreach ($dups as $dup=>$count) {
    if ($count>1) {
      echo "$dup was duplicated $count times<br />\n"; 
    }
  } 
}
?>
<form name="form1" method="post" action="">
  <input name="text2" type="text">
  <input type="submit" name="Submit" value="Submit">
</form>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ddrudikCommented:
Thanks for the question and the points.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.