phpregexdocblocks

How would I write regex for getting the variable type from a variable comment?


I'm looking for a regex string to parse a property type from it's comment as below.

/**
 * Identity
 * 
 * @var integer
 */
protected $id;

I'm using the ReflectionProperty class to get the comment as a string as the var dump below:

string(55) "/** * Identity * * @var integer */"

How would use the regex to return the type after @var and nothing else.

Thanks


Solution

  • The proposed solution in the comments seems rather excessive... should be much easier like this - as types never contain spaces, just match until the space.

    /@var\s*([^\s]+)/i
    

    https://regex101.com/r/xM6kL3/2