0

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.

ShawnDaGeek
  • 4,145
  • 1
  • 22
  • 39
  • i was hoping to thank both of you for your time and troubles. So far the filter is returning the arrays i need, and the removal of the @[] has not failed yet. I had to mix both of your solutions to get all this to work, so with out both of you i would not have gotten this. – ShawnDaGeek Jul 05 '12 at 17:27

2 Answers2

1
<?php

$string = "@[123:456:abcZ]";
preg_match('/^@\[(([0-9]+:){2}[a-zA-Z]+)\]$/', $string, $matches);

echo $matches[1];  
keelerm
  • 2,873
  • 20
  • 12
  • Warning: preg_match() expects parameter 2 to be string, array given in /home/content/06/8649306/html/feed.php on line 114 – ShawnDaGeek Jul 04 '12 at 20:23
  • 1
    That's interesting. It runs without warning on my box. In the example provided, the second parameter is clearly a string. Can you please provide the snippet you're running. – keelerm Jul 04 '12 at 20:26
  • ya i was trying to return the array created with return $matches[1]; and return $matches, but the first returns nothing at all, the later returns error. – ShawnDaGeek Jul 04 '12 at 20:28
  • 1
    That snippet is just a simple example. If you're going to use it, you need to verify that the preg_match actually returned a result. Try something like if ( preg_match('/^@\[(([0-9]+:){2}[a-zA-Z]+)\]$/', $string, $matches) ) { return $matches[1]; } else { return ""; } – keelerm Jul 04 '12 at 20:30
  • i am going to try to json encode then decode after the return. – ShawnDaGeek Jul 04 '12 at 20:30
  • 1
    Also to handle the additional whitespace you updated the example to include change the pattern to /^@\[(([0-9]+:){2}[a-zA-Z ]+)\]$/ . If there are other characters you're anticipating besides alpha and space, you can easily modify it as necessary. – keelerm Jul 04 '12 at 20:33
  • Warning: preg_match() [function.preg-match]: Compilation failed: unmatched parentheses at offset 12 in /home/content/06/8649306/html/feed.php on line 191 – ShawnDaGeek Jul 04 '12 at 20:39
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/13432/discussion-between-keelerm-and-shawn-e-carter) – keelerm Jul 05 '12 at 04:26
0

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

ohaal
  • 5,208
  • 2
  • 34
  • 53
  • i added a sample $str where spaces in 3rd delimiter cause it not to replace. – ShawnDaGeek Jul 04 '12 at 20:20
  • 1
    Ah, I thought anyLetters would be only letters, it's a simple fix. Fixed it now by adding a space in the character class. – ohaal Jul 04 '12 at 22:38