SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Remote Dashes withen they appear betwen a letter and number or nuumber and letter

    I am trying to remove dashes from a string. Simple enough, using preg_replace()

    but in this case, I want to remove dashes ONLY when they appear between a letter OR between a number and a letter.

    I am not too comfy with regex, expecially when I can't use Postix expressions.

    Say given a string like "123-ab" or "ab-123" I want to remove the dashes such that the strings returned are "123ab" and "ab123" respectively.

    I think I can do this with the preg_replace function. Your direction is appreciated.

  2. #2
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    <?php
    echo preg_replace(
        
    '~(?<=[a-z\d])-(?=[a-z\d])~i',
        
    null,
        
    '-5445-fsd-4234-vxcv-'
    );
    #-5445fsd4234vxcv-
    ?>
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  3. #3
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Whoa! Anthony!

    *bows* Much Respect for your regex skills!

    Your script works beautifully, except for the case when the hyphen is between letters or between numbers. IN that case, I don't want to remove the hyphen.

    Can I brake it down into two difference calls to preg_replace?

  4. #4
    SitePoint Addict
    Join Date
    Mar 2005
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, breaking it up into two steps, I got the results I was looking for.

    Take
    Code PHP:
    $string1 = "--this is-123-4-aaa--"
     
    $string1 = preg_replace(
        '~(?<=[a-z])-(?=[\d])~i',
        null,
        $string1);
     
    //produces 
    //--this is123-4-aaa--
    then I take that
    and do
    Code PHP:
    preg_replace(
        '~(?<=[\d])-(?=[a-z])~i',
        null,
        $string1
    )
     
    //which produces 
    //--this is123-4aaa--

    Thank you very much for the guidance and example!

  5. #5
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,397
    Mentioned
    61 Post(s)
    Tagged
    0 Thread(s)
    Your two patterns could be combined into a single pattern; here's one example, of course there are numerous other ways to achieve the same result but this one should hopefully be fairly clear (as far as this type of regex goes) about what it does.

    PHP Code:
    echo preg_replace(
        
    '/-(?<=\d-(?=[a-z])|[a-z]-(?=\d))/i',
        
    '',
        
    '-123-abc-456-789-def-ghi-'
    );
    // -123abc456-789def-ghi- 
    Salathe
    Software Developer and PHP Manual Author.


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
  •