Search code examples
phpregexpreg-matchpreg-match-allpreg-replace-callback

preg_match for certain part of the comments


i want to find certain part of the comments. following are some examples

/*
 * @todo name another-name taskID
 */

/* @todo name another-name taskID */

currently im using following regular expression

'/\*[[:space:]]*(?=@todo(.*))/i'

this returns following:

* @todo name another-name taskID
or
* @todo name another-name taskID */

how can i just get just @todo and names, but not any asterisks?

desired output

@todo name another-name taskID
@todo name another-name taskID

Solution

  • Try this:

    $str=<<<STR
    /*
     * @todo name another-name taskID 1
     */
    
    /* @todo name another-name taskID 2 */
    STR;
    $match=null;
    preg_match_all("/\*[[:space:]]*(@todo[^(\*\/$)]*)/i",$str,$match,PREG_SET_ORDER);
    print_r($match);
    

    echos

    Array
    (
        [0] => Array
            (
                [0] => * @todo name another-name taskID 1
    
                [1] => @todo name another-name taskID 1
    
            )
    
        [1] => Array
            (
                [0] => * @todo name another-name taskID 2 
                [1] => @todo name another-name taskID 2 
            )
    
    )
    

    Not a perfect solution (notice the line-ending in $match[0][0]) though.