php regex how to remove duplicate charactors and make every charactor unique in string
198
How should one usepreg_replace()
to replace a string from 'aabbaacc' to 'abc'?
Currently, my code usesstr_split()
thenarray_unique()
thenimplode()
.
I thinkpreg_replace()
can achieve this also, but I don't know how.
Thank you for your help.
Answer
Solution:
A regex that seems to work for me is
/(.)(?=.*?\1)/
. Please test it for yourself here:http://regexpal.com/
I've also tested it with
preg_replace('/(.)(?=.*?\1)/', '', 'aaabbbabc')
which returns the expectedabc
.Hope this helps :)
Answer
Solution:
This is the closest I got. However, it's basically a copy of : How do I remove duplicate characters and keep the unique one only in Perl?
Unfortunately, it does not keep the string in the same order. I don't know if that is important to you or not.
Answer
Solution:
try this
output
or this with out Regex
output: