Looking for a regular expression for preg_match_all
that can detect a variable encapsulated in a % in a body of text like so:
Lorem ipsum dolor sit amet, consectetur adipisicing
%variable1%
, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip%variable2%
ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa%variable3%
qui officia deserunt mollit anim id est laborum.
Where the variables to be detected are encapsulated in % symbols. The variables can include lowercase letters (a-z), numbers (0-9) and the hyphen (-). I would like to return the names of the variables in the % signs in an array like follows, but cant get it to match.
Array (
[0] => 'variable1',
[1] => 'variable2',
[3] => 'variable3'
}
Any advice?
/%(.+?)%/
, then refine. – elclanrs~%([^%]+)%~
is more explicit – HamZa