473,398 Members | 2,088 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Want to remove duplicates in a string

Hello again, I have one further question about improving a validation.
Currently this will validate a string of words separated by commas, and
if it's greater than 9 will do something, or something else if less
than 9. How can I improve this, so before it does the checking remove
any duplicates in the string?

<?
$tmp = explode(',',$your_str);
if (count($tmp) > 9) {
// do something

} else {

// do something else

}
?>

Jul 17 '05 #1
6 9478
Ra*********@hotmail.com wrote:
Hello again, I have one further question about improving a validation.
Currently this will validate a string of words separated by commas, and
if it's greater than 9 will do something, or something else if less
than 9. How can I improve this, so before it does the checking remove
any duplicates in the string?

<?
$tmp = explode(',',$your_str);
if (count($tmp) > 9) {
// do something

} else {

// do something else

}
?>

$tmp = array_unique(explode(',', $your_str));

By the way, if it doesn't become unreadable to you, and you don't need
the $tmp variable later on, you could do away with $tmp and use this
directly:

if (count(array_unique(explode(',', $your_str))) > 9)
HTH,
JP

--
Sorry, <de*****@cauce.org> is a spam trap.
Real e-mail address unavailable. 5000+ spams per month.
Jul 17 '05 #2
Ra*********@hotmail.com wrote:
Hello again, I have one further question about improving a validation.
Currently this will validate a string of words separated by commas,
and if it's greater than 9 will do something, or something else if
less than 9. How can I improve this, so before it does the checking
remove any duplicates in the string?


$tmp = array_unique(explode(',',$your_str));
JW

Jul 17 '05 #3
Hello! Well, I need to keep the original string intact with its comma
delimiters. I need this to read that string, remove all duplicate key
phrases, and then check if 9 or more unique key phrases are in there.
If it then passes it should do something, otherwise something else.

Jul 17 '05 #4
.oO(Ra*********@hotmail.com)
Hello! Well, I need to keep the original string intact with its comma
delimiters.


The posted code doesn't touch the original string.

Micha
Jul 17 '05 #5
I am still pulling my hair out with this. :O I don't want to lose the
commas, or keep it in an array right now, I want to store it in a
database with duplicates removed, so if someone inputs:

"green,blue,red,blue,green"

I want to get back a string with:

"green,blue,red"

Michael Fesser wrote:
.oO(Ra*********@hotmail.com)
Hello! Well, I need to keep the original string intact with its commadelimiters.


The posted code doesn't touch the original string.

Micha


Jul 17 '05 #6
I noticed that Message-ID:
<11*********************@z14g2000cwz.googlegroups. com> from
Ra*********@hotmail.com contained the following:
I am still pulling my hair out with this. :O I don't want to lose the
commas, or keep it in an array right now, I want to store it in a
database with duplicates removed, so if someone inputs:

"green,blue,red,blue,green"

I want to get back a string with:

"green,blue,red"


<?php
$string="green,blue,red,blue,green,green,red,blue" ;

$string_array=explode(",","$string");

$output[]=$string_array[0];

for($i=1;$i<count($string_array);$i++){
if(!in_array($string_array[$i],$output)){
$output[]=$string_array[$i];
}
}

print implode(",",$output);

?>

--
Geoff Berrow (put thecat out to email)
It's only Usenet, no one dies.
My opinions, not the committee's, mine.
Simple RFDs http://www.ckdog.co.uk/rfdmaker/
Jul 17 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Wm | last post by:
I have a table of users in mySQL that appears to have a lot of duplicates. What's the best way to look at the userID and email and delete the duplicates? Thanx, Wm
7
by: Voetleuce en fênsievry | last post by:
Hello everyone. I'm not a JavaScript author myself, but I'm looking for a method to remove duplicate words from a piece of text. This text would presumably be pasted into a text box. I have,...
3
by: bellefy | last post by:
Hi All, I have a fairly large table with approx 30K rows that updates every night via a cron script that automatically downloads the 2 new csv's. The problem is the files are downloaded from...
2
by: Saintor | last post by:
I have a string of email addresses with duplicates. like "abc@abc.com; cde@cde.com; abc@abc.com" If I use this string with sendobject, the receiver will get it only once which is perfect to...
3
by: Quack Boy | last post by:
I'm new to MS Access (97) and I need for a query to find *near* matches (not exact duplicates) mainly to weed out duplicates that may contain similar erronious data. EG Table1 ...
6
by: Marlene | last post by:
Hi All I have the following scenario, where I have found all the duplicates in a table, based on an order number and a part number (item).I might have something like this: Order PODate Rec...
11
by: steve smith | last post by:
Hi I'm still having some problems getting my head round this language. A couple of things don't seem to work for me. First I am trying to obtan a count of the number of words in a sting, so am...
6
jlandbw04
by: jlandbw04 | last post by:
Okay. Here's the deal. I have this assignment for college that has me completely puzzled. I need this assignment to do the following: 1. input 12 integers into an array from the user. 2. output...
2
by: oaklander | last post by:
I currently have two String variables I check to find if they are duplicates: String str1 = "red"; String str2 = "yellow"; if (str1.equals(str2)){ System.out.println("Duplicate"); }...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.