Preg_match for a specific part of comments

I want to find a certain part of the comments. Below are some examples

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

im currently using the following regex

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

this returns the following:

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

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

desired output

 @todo name another-name taskID @todo name another-name taskID 
+4
source share
1 answer

Try the following:

 $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); 

echoes

 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 (note the end of the line in $match[0][0] ).

+1
source

Source: https://habr.com/ru/post/1445683/


All Articles