[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Find duplicates in string

Posted on 2011-04-20
2
Medium Priority
?
275 Views
Last Modified: 2012-06-27
Hi,

I've got the string:
123(05)-333(12)-2354(13)-2354(14)-3333(50)
And I need a script to remove duplicated numbers from the string, but with ignoring the brackets. In this case from string above should be removed '-2354(14)'
I know I need to explode the string by '-' first, but what next?

Thanks for any help.
0
Comment
Question by:Zado
2 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 2000 total points
ID: 35434512
You can try this:
$str    = '123(05)-333(12)-2354(13)-2354(14)-3333(50)';
$result = '';
$found  = array();
foreach (explode('-', $str) as $part) {
    if (!preg_match('/(\d+)\((\d+)\)/', $part, $matches)) {
        continue;
    }
    if (in_array($matches[1], $found)) {
        continue;
    }
    $found[] = $matches[1];
    $result .= ($result != '' ? '-' : '') . $part;
}

echo $result;

Open in new window

0
 
LVL 8

Author Comment

by:Zado
ID: 35434570
Great, thank you.
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month17 days, 20 hours left to enroll

830 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