SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Hybrid View

  1. #1
    SitePoint Member
    Join Date
    Mar 2007
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    preg_match for confusing string

    Hello,

    I really need some help with following string:

    M1;, Bedfordshire, Flitwick, M1; 12, A5120;

    What I ideally want is for the string to look like:

    M1, Bedfordshire, Flitwick, M1 - Junction 12, A5120

    So basically I need to match the string part: M1; 12 and turn it into M1 - Junction 12 and then match any remaining semi-colon and delete it.

    Please help as I have spent a couple of days scratching my head on this one.

    Thanks,

    Justin.

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Code php:
    $s = 'M1;, Bedfordshire, Flitwick, M1; 12, A5120;';
    $s = preg_replace('/([A-Z][0-9]); (\d+)/', '\\1 - Junction \\2', $s)
    # M1;, Bedfordshire, Flitwick, M1 - Junction 12, A5120;
    $s = str_replace(';', '', $s);
    # M1, Bedfordshire, Flitwick, M1 - Junction 12, A5120

    Or
    Code php:
    $s = 'M1;, Bedfordshire, Flitwick, M1; 12, A5120;';
    $s = preg_replace('/([^;]+);, ([^,]+), ([^,]+), ([^;]+); ([^,]+), ([^;]+);/', '\\1, \\2, \\3, \\4 - Junction \\5, \\6', $s);
    # M1, Bedfordshire, Flitwick, M1 - Junction 12, A5120
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.



Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •