views:

36

answers:

1

Hi I have the following code but im not a big fun of reg exp as they are too confusing:

<?php
$r = '|\\*(.+)\\*|'; 
$w = '';
$s = 'hello world *copyMe* here'; 
function callbk($str){
    print_r($str);
    foreach($str as $k=>$v) {
        echo $v;
    }
}
$t = preg_replace_callback($r,'callbk',$s);

//output: Array ( [0] => *copyMe* [1] => copyMe ) *copyMe*copyMe


?>

my question is why is there both "*copyMe*" and "copyMe"? i was hoping to get either one or the other, not both. any help would be appriciated.

+1  A: 

Because you are using a capturing expression (). If you omit the brackets you will only get *copyMe*.

Jakub Hampl
nice one :) I thought brackets group stuff but that did the trick ;)
Val
Also just a quick note: it's considered best practice to write your Regexp with `/` as the delimiters, not `|`.
Jakub Hampl