The mode will be easier:
$input = 'foo_left.jpg'; if(!preg_match('/_(left|right|center)/', $input, $matches)) { // no match } $pos = $matches[0]; // "_left", "_right" or "_center"
Look in action .
Update:
For a more secure approach (if there can be multiple instances of "_left" and friends in the file name), you might consider adding to the regular expression.
This will only match if a dot follows l / r / c:
preg_match('/(_(left|right|center))\./', $input, $matches);
This will only match if l / r / c is followed by the last dot in the file name (which practically means that the base name ends with the l / r / c specification):
preg_match('/(_(left|right|center))\\.[^\\.]*$/', $input, $matches);
And so on.
If you use these regular expressions, you will find the result in $matches[1] instead of $matches[0] .
source share