Solved

Detect repeat words in string

Posted on 2009-07-09
3
409 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 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

726 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