0

I have the following code but I'm not a big fan 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 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

2 Comments

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.