Solved

Detect repeat words in string

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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
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…
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…
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.

706 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

19 Experts available now in Live!

Get 1:1 Help Now