1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

need some regexp help

Discussion in 'PHP' started by nlopes, Jun 1, 2004.

  1. #1
    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:
    
    $str = '"%Y-%m-%d" " " "%H:%M:%S"
    NULL
    "1"
    "/tmp"';
    
    echo preg_replace('/^"[^"]*(["]+).*"$/', '', $str);
    
    PHP:
    Note: The above code doesn't work (obviously).

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

    Thanks,
    Nuno
    SEMrush
     
    nlopes, Jun 1, 2004 IP
    SEMrush
  2. Help Desk

    Help Desk Well-Known Member

    Messages:
    1,365
    Likes Received:
    25
    Best Answers:
    0
    Trophy Points:
    180
    #2
    It's not sure if this handles all the possible cases you are looking to handle, but have you tried this?

    $str = '"%Y-%m-%d" " " "%H:%M:%S" 
    NULL 
    "1" 
    "/tmp"'; 
    
    echo preg_replace('/" "/', '', $str); 
    Code (markup):
     
    Help Desk, Jun 1, 2004 IP
  3. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks, but it doesn't work... it simplys echoes '': nothing!

    I need a regexp to remove the quotes " in the string (but not the quotes in the end and in the begining). and to remove those extra spaces, too.

    Any ideas?
     
    nlopes, Jun 1, 2004 IP
  4. Arnica

    Arnica Peon

    Messages:
    320
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi nlopes

    To save complexity in the reg expression I would simply use it to replace all ' " ' in the string and then prepend and append ' " ' afterwards. That way you're not having to worry about 'last matches' etc.

    Mick :)
     
    Arnica, Jun 1, 2004 IP
  5. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That would broke strings like NULL, that don't have quotes...
     
    nlopes, Jun 1, 2004 IP
  6. digitalpoint

    digitalpoint Overlord of no one Staff

    Messages:
    38,284
    Likes Received:
    2,600
    Best Answers:
    460
    Trophy Points:
    710
    Digital Goods:
    29
    #6
    Actually this does works for me:
    <?php
            $str = '"%Y-%m-%d" " " "%H:%M:%S"
    NULL
    "1" 
    "/tmp"';
    
            echo preg_replace('/" "/', '', $str);
    ?>
    PHP:
    Although, probably better to use str_replace(), like so:
    <?php
            $str = '"%Y-%m-%d" " " "%H:%M:%S"
    NULL
    "1"
    "/tmp"';
    
            echo str_replace('" "', '', $str);
    ?>
    PHP:
     
    digitalpoint, Jun 1, 2004 IP
  7. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thanks Shawn. Your str_replace() might work. I'll test it tomorrow in my program.

    Bah,... But I really wanted some complicated regexp :) Something less specific to this case.... But lets see if your solution works!! :D
     
    nlopes, Jun 1, 2004 IP
  8. Arnica

    Arnica Peon

    Messages:
    320
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #8
    How's this for complicated?

    <?php 
    		$str = '"%Y-%m-%d" " " "%H:%M:%S" 
    NULL 
    "1" 
    "/tmp"'; 
    		echo preg_replace('/(\")?([^\"]+)(?:\"| )*([^\"]* (?:\"?[\n\r\f]?))/g', '$1$2 ', $str); 
    
    ?>
    PHP:
    Don't do PHP (Die hard Javascript ASP programmer :eek: ) but this should do the job and handle any variations to the input. Let me know if it does.

    NB There is some overkill here in that the ' " ' don't need to be escaped and you could probably get away with fewer parameters to detect the line break!

    Mick
     
    Arnica, Jun 1, 2004 IP
  9. nlopes

    nlopes Guest

    Messages:
    103
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thank you for your help!

    I've made a general preg_match, which is what I wanted:
    
        if(preg_match_all('/"([^"]+)"/S', $default, $match) > 1) { 
          $st ='';
    
          foreach($match[1] as $add) {
            $st .= $add;
          }
          $default = '"' . $st . '"';
        }
    
    PHP:
    Full program at: http://testes.aborla.net/ini-update.php
     
    nlopes, Jun 2, 2004 IP
  10. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #10
    up..... :D:D
     
    bartolay13, Feb 25, 2010 IP
  11. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #11
    <?php
    
    $input_data = <<<INPUT
    "%Y-%m-%d" " " "%H:%M:%S"
    NULL
    "1"
    "/tmp"
    INPUT;
    
    echo "<pre>".preg_replace("/\"(.*)\" \" \" \"(.*)\"/s", "\"$1 $2\"", $input_data)."</pre>";
    
    ?>
    PHP:
     
    danx10, Feb 25, 2010 IP