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
Solved

Detect repeat words in string

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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
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…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

792 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