Find duplicates in string

Posted on 2011-04-20
Last Modified: 2012-06-27

I've got the string:
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.
Question by:Zado
    LVL 48

    Accepted Solution

    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)) {
        if (in_array($matches[1], $found)) {
        $found[] = $matches[1];
        $result .= ($result != '' ? '-' : '') . $part;
    echo $result;

    Open in new window

    LVL 8

    Author Comment

    Great, thank you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
    The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

    794 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

    Need Help in Real-Time?

    Connect with top rated Experts

    17 Experts available now in Live!

    Get 1:1 Help Now