[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

array_unique($words);

Posted on 2008-02-07
16
Medium Priority
?
274 Views
Last Modified: 2013-12-13
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>
0
Comment
Question by:mvanthof
  • 6
  • 5
  • 5
16 Comments
 
LVL 6

Expert Comment

by:paulp75
ID: 20840304
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
 

Author Comment

by:mvanthof
ID: 20840345
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
 
LVL 6

Expert Comment

by:paulp75
ID: 20840391
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:mvanthof
ID: 20840446
0
 
LVL 6

Expert Comment

by:paulp75
ID: 20840646
to make it only show => 3 chars change line 9 to this


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

Open in new window

0
 
LVL 6

Expert Comment

by:paulp75
ID: 20840677
oops just try this

if ($word == $duplicate && (strlen($word) > 2)) {
0
 

Author Comment

by:mvanthof
ID: 20840709
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
 

Author Comment

by:mvanthof
ID: 20840721
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
 
LVL 6

Expert Comment

by:paulp75
ID: 20840778
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
 
LVL 6

Expert Comment

by:paulp75
ID: 20840853
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
 

Author Comment

by:mvanthof
ID: 20840911
now it's not even working...
0
 
LVL 27

Expert Comment

by:ddrudik
ID: 20859105
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
 
LVL 27

Expert Comment

by:ddrudik
ID: 20859136
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
 
LVL 27

Expert Comment

by:ddrudik
ID: 20859706
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
 
LVL 27

Accepted Solution

by:
ddrudik earned 1600 total points
ID: 20859730
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
 
LVL 27

Expert Comment

by:ddrudik
ID: 20865391
Thanks for the question and the points.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

612 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question