Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

Creating Natural Names from Special Strings

Last post 06-29-2008, 12:13 PM by ddrudik. 1 replies.
Sort Posts: Previous Next
  •  06-29-2008, 3:17 AM 43578

    Creating Natural Names from Special Strings

    Hi All,

    I am not very expert in regex, so please be patient. I am using an audio tagging software (MP3Tag) with scripting function in it.

    Here is my problem. I have a string like this:

    Doe, John Arthur~Munkova-Sanchez, Sophia~Koshi~Smith, Peter~Umba Tres, Kris

    as you can see it contains names sepatated by a ~. The last name(s) and the first name(s) (if any) are separated by a comma. The space after the comma is not mandatory. Names can contain letters, apostrophs, periods, hyphens and spaces. The number of names can be one, or several.

    I should create a string containing the names in natural sort, separated by a " / ", for example.

    John Arthur Doe / Sophia Munkova-Sanchez / Koshi / Peter Smith / Kris Umba Tres

     I tried this code:

    $regexp($regexp(STRING~,(.+?)','\s*(.+?)~,$2 $1 / ), / $,)

    but it doesn't work with the names without a comma (like Koshi in my example)

    Any help will be appreciated.

    Stevest

  •  06-29-2008, 12:13 PM 43582 in reply to 43578

    Re: Creating Natural Names from Special Strings

    Not sure how this would be done without code, here's how this would be done with code in PHP:

    <?php
    $sourcestring='Doe, John Arthur~Munkova-Sanchez, Sophia~Koshi~Smith, Peter~Umba Tres, Kris';
    $matches=preg_split('/~/',$sourcestring);
    sort($matches);
    function reorder($item) {
      if (preg_match('/(.*?), *(.*) */',$item,$match)) {
        return $match[2].' '.$match[1];
      } else {
        return $item;
      }
    }
    $result=implode('/',array_map('reorder',$matches));
    echo $result;
    ?>


View as RSS news feed in XML