473,795 Members | 2,854 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(',',$yo ur_str);
if (count($tmp) > 9) {
// do something

} else {

// do something else

}
?>

Jul 17 '05 #1
6 9520
Ra*********@hot mail.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(',',$yo ur_str);
if (count($tmp) > 9) {
// do something

} else {

// do something else

}
?>

$tmp = array_unique(ex plode(',', $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_un ique(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*********@hot mail.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(ex plode(',',$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************ *********@z14g2 000cwz.googlegr oups.com> from
Ra*********@hot mail.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,re d,blue,green"

I want to get back a string with:

"green,blue,re d"


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

$string_array=e xplode(",","$st ring");

$output[]=$string_array[0];

for($i=1;$i<cou nt($string_arra y);$i++){
if(!in_array($s tring_array[$i],$output)){
$output[]=$string_array[$i];
}
}

print implode(",",$ou tput);

?>

--
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
7966
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
23936
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, for example, a list of town names, but there are hundreds of duplicates, like: "Aberdeen Aberdeen Aberdeen Edinburg Edinburg Inverness etc."
3
4760
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 two completely different systems and I've recently found duplicates! To make matters worse, the two feeds do not share a common unique key.
2
2344
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 me. I wonder if it will be the case with other mail applications than Outlook Express. TIA (BTW if somebody has code to delete duplicates in a string like this, it would be appreciated.)
3
2736
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 Name Company Town Alan ABC Alphaville Brian B inc. Beta City Calan ABC Alpaville
6
2405
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 Qty Invoice# Item Supplier Status POReceivedDate 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004 570133 03/09/2004 50 0 DMEDIUM L0010 PENDING 03/09/2004
11
5839
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 using the split function with ' ', but how do i get it to take into account punctuation marks such as ',',',' etc? Also I am then trying to add contents of an array of strings to an arraylist, but only if the string isn't already there. I was using...
6
2064
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 the numbers back to the screen showing what the user input. 3. ask the user what integer they would like to remove. 4. use a loop to remove ALL occurrance of that integer, and the output the new numbers back to the screen for the user to see the...
2
1679
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"); } else{ System.out.println("Not duplicate");
0
9673
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9522
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10448
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10217
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
10003
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9046
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7544
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5440
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4114
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.