Solved

Detect repeat words in string

Posted on 2009-07-09
3
395 Views
Last Modified: 2012-08-13
Hi E's, I have one string that sometimes have a repeat words, and I want to know how detect them, and put them in other variable.
In practice:
$string = "cabanas pesca . fotografias algarve tavira cabanas . cabanas tavira fotografias . ser portugues . moncarapacho algarve . vila moncarapacho fotos . vila moncarapacho . free photos . foto fotografia . paisagem urbana . paisagem ceu . paisagem montanha . paisagem rural . concurso fotos paisagem . fotografia digital . foto paisagem . fotografia paisagem .";

The goal is detect repeat words and put them in new variable:
$newvariable = "cabanas fotografias algarve moncarapacho fotos foto paisagem";

How I do that?

Regards, JC
0
Comment
Question by:Pedro Chagas
  • 2
3 Comments
 
LVL 39

Expert Comment

by:Roger Baklund
ID: 24819427
Why is not "tavira", "vila" and "fotografia" in the result?
0
 
LVL 3

Author Comment

by:Pedro Chagas
ID: 24819546
@cxr, I'm sorry, but when I see the string that words scape from my eyes!
Yes, tavira, vila and fotografia is for put in the result.
$newvariable = "cabanas fotografias algarve tavira moncarapacho vila fotos foto paisagem fotografia";

Regards, JC
0
 
LVL 39

Accepted Solution

by:
Roger Baklund earned 500 total points
ID: 24819554
Then try this:
$string = "cabanas pesca . fotografias algarve tavira cabanas . cabanas tavira fotografias . ser portugues . moncarapacho algarve . vila moncarapacho fotos . vila moncarapacho . free photos . foto fotografia . paisagem urbana . paisagem ceu . paisagem montanha . paisagem rural . concurso fotos paisagem . fotografia digital . foto paisagem . fotografia paisagem .";
 

$words = explode(' ',$string);

$counts = array_count_values($words);

$multiple = array_filter($counts,create_function('$item','return $item>1;'));

unset($multiple['.']);

$newvariable = implode(' ',array_keys($multiple));

echo $newvariable

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

910 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now