SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2009
    Posts
    1
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    need some regexp help

    Hello,

    I need some regexp help, using preg_replace.

    Input data:
    "%Y-%m-%d" " " "%H:%M:%S"
    NULL
    "1"
    "/tmp"

    ... and so one...

    and it should ouput this:
    "%Y-%m-%d %H:%M:%S"
    NULL
    "1"
    "/tmp"


    Without using some nasty PHP commands, is there a regexp to replace those chars?, like:

    Code:
    $str = '"%Y-%m-%d" " " "%H:%M:%S"
    NULL
    "1"
    "/tmp"';
    
    echo preg_replace('/^"[^"]*(["]+).*"$/', '', $str);
    Note: The above code doesn't work (obviously).

    Probably this could be done with some lookahead/lookbehind subexpressions?

    Thanks,

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,578
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Ya don't need a regular expression to remove a literal string

    PHP Code:
    echo str_replace('" " " "'' '$str); 

  3. #3
    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)
    A few ways come to mind, but I much prefer the simple str_replace given the sample data.
    PHP Code:
    <?php
    $sString 
    '"%Y-%m-%d" " " "%H:%M:%S"';

    echo 
    preg_replace('~(?<="%Y-%m-%d)([\\s"]+)(?=%H:%M:%S")~'' '$sString); #"%Y-%m-%d %H:%M:%S"

    echo preg_replace('~(" " " ")~'' '$sString); #"%Y-%m-%d %H:%M:%S"

    echo str_replace('" " " "'' '$sString); #"%Y-%m-%d %H:%M:%S"
    ?>
    @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.


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
  •