Dynadot Aftermarket: discover domain gems worth investing in. Visit our aftermarket.

Remove duplicates from a textbox

Namecheap AuctionsNamecheap Auctions
SpaceshipSpaceship
NamecheapNamecheap
Watch

jdk

VIP Member
Impact
100
How do I create a textbox and have a button to click and it will take and remove any duplicates in a textbox.

It should remove blank lines too before removing the dupes. I haven't been able to find anything with doing it using a textbox and PHP.
 
0
Close
The views expressed on this page by users and staff are their own, not those of NamePros.
GoDaddyGoDaddy
0
Close
0
Close
jdk said:
It should remove blank lines too before removing the dupes. I haven't been able to find anything with doing it using a textbox and PHP.

Just a note, php has absolutely no knowledge that the data has come from a textbox. All it knows is that it has certain data held in a variable.

Danltn said:
Did this for you. Most recent post on my blog:

http://danltn.com/remove-all-duplicate-words-204/

Enjoy. :)

Just had a look at your example, it might be an idea for to add the ability to enable case insensitive searches as well (your current example is case sensitive)
 
0
Close
peter@flexiwebhost said:
Just a note, php has absolutely no knowledge that the data has come from a textbox. All it knows is that it has certain data held in a variable.



Just had a look at your example, it might be an idea for to add the ability to enable case insensitive searches as well (your current example is case sensitive)

Hmm, I used a quick strtolower but I'm interested in extending this, do you have any suggestions?
 
0
Close
there is no string to lower function used in your example.

Ideally you should encompass the script within a function. You could make the function have 2 inputs the 1st for the data and the second an optional 1 for case. Then simply use an if statement to determine if it is case sensitive.

PHP:
function unique_words($data, $case_sensitive='0')
{
	if ($case_sensitive==1)
	{
		$data = strtolower($data);
	}
	$data = explode(" ", str_replace("\n", " ", $data));
	return array_unique($data);
}

Using the stripslashes and htmlentities is not necessary within the function as they are unrelated to the problem.

You have stripped slashes in your example but you never checked magic_quotes_gpc was enabled. If it is not then you are trying to strip valid data which could cause unexpected output (if the input was meant to contain a slash for example you would have removed it)

Also when testing I noticed from your example that “the is 1 entry and as” is another was this as intended (i left the behavior in my example).

Of course if you wish the function to output the words as a string then it would be easy to do but I left it as an array for now.
 
0
Close
CatchedCatched

We're social

Escrow.com
Spaceship
Domain Recover
CryptoExchange.com

NamePros updates

Switch between accounts
You can now stay logged in to more than one NamePros account in the same browser. Click your avatar, then "Accounts in this browser", to...
AIVikings
Catchy
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back