preg-matchphp-5.3strtr

simple preg_match regex needed


I am trying to preg match for tags in my strings that contain @[anyNumbers:anyNumbers:anyLetters] i am hoping to remove the @[] leaving everything inbetween as a string that i can later filter.
What would be the easiest way to accomplish this?

$str = 'Have you heard of the @[159208207468539:274:One Day without Shoes] (ODWS) campaign?  ODWS is an annual initiative by @[8416861761:274:TOMS] to bring awareness around the impact a pair of shoes can have on a child's life.';
    function gettag($text){
    //
            //$regex = "\@[([a-z0-9-:]*)\]";
            //$match = preg_match("/^$regex$/", $text);
                    //return $match;
                    return preg_replace('/@\[(\d+:\d+:[a-zA-Z]+)\]/', '${1}', $text);

    }
    gettag($str);

returns

Have you heard of the @[159208207468539:274:One Day without Shoes] (ODWS) campaign? ODWS is an annual initiative by 8416861761:274:TOMS to bring awareness around the impact a pair of shoes can have on a child's life.


Solution

  • Using slightly modified version of your pattern, you can do this:

    function gettag($text) {
        return preg_replace('/@\[([ a-z0-9-:]*)\]/i', '${1}', $text);
    }
    

    If you'd like it to be even more specific, we can modify the pattern to be stricter:

    function gettag($text) {
        return preg_replace('/@\[(\d+:\d+:[a-zA-Z ]+)\]/', '${1}', $text);
    }
    

    See it on codepad