SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    regular expression problem

    Hello

    I have this string

    $output="Jan-19-12 03:11:31 [Various_Words] 85.54.177.0 -- invalid HELO: 'PC100521998230'";

    I need to remove from $output [Various_Words] to have this

    $output="Jan-19-12 03:11:31 85.54.177.0 -- invalid HELO: 'PC100521998230'";

    Into the brackets there could be various words not only Various_Words .

    I am trying this

    $pattern = '/ \[\[A-Za-z]\]/' ;
    $replacement =' ';
    $output = preg_replace($pattern, $replacement, $output) ;

    but the pattern is not good, any help please ?

    Thank you
    trcris ::> http://www.trcris.com

  2. #2
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This will do the trick. It matches the parts of the $output that you want to keep and puts it in to a variable.

    PHP Code:
    <?php

    $output 
    "Jan-19-12 03:11:31 [Various_Words] 85.54.177.0 -- invalid HELO: 'PC100521998230'";
    $regex "#^([a-z]{3}\-\d{2}\-\d{2} \d{2}:\d{2}:\d{2}) .+ (\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3} \-\- invalid HELO: '.+')$#Ui";

    preg_match($regex$output$match);

    if(
    count($match) == 3)
    {
        
    $desired_output $match[1].' '.$match[2];
        
    var_dump($desired_output);
    }
    else
    {
        die(
    'Did not match the correct pattern.');
    }

    ?>
    This code outputs:

    string(64) "Jan-19-12 03:11:31 85.54.177.0 -- invalid HELO: 'PC100521998230'"

  3. #3
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thank you but my question was more general

    suppose I have

    $output="alex domain [test] sdfsdf [pippo] sdfdsf";

    I should have as result this

    $output="alex domain sfsdf sdfdsf";

    which is the required regular expression ?

    I am trying

    \[[a-zA-Z]\]

    / \[\[A-Za-z]\]/

    none seems to work ..

    Thank you
    trcris ::> http://www.trcris.com

  4. #4
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am getting help from http://gskinner.com/RegExr/ now , however I can't find a way
    trcris ::> http://www.trcris.com

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2011
    Posts
    47
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you just want to remove stuff in square brackets, try the following:

    PHP Code:
    $output "alex domain [test] sdfsdf [pippo] sdfdsf";
    $output preg_replace("#\[[a-zA-Z0-9]+\]#"""$output);
    var_dump($output); 

  6. #6
    SitePoint Addict
    Join Date
    Feb 2004
    Location
    Rome
    Posts
    296
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I need exactly this, thank you!
    trcris ::> http://www.trcris.com


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
  •