?
Solved

Detect repeat words in string

Posted on 2009-07-09
3
Medium Priority
?
414 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! Learn more.

Question has a verified solution.

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

This article discusses how to implement server side field validation and display customized error messages to the client.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

770 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