3

What's the best way to achieve the following:

I have a $img variable containing e.g. myimage_left.jgp, someimage_center.jpg or img_right.jpg

What's the best way to test for _left, _right or _center of the filename and extract this value and store it in a variable?

So I have $img which already contains the complete basename of the file. And I need to have $pos which should hold _center or _left or _right.

What's the way to do that? preg_match, strpos, etc?

4 Answers 4

12

A regex would be simplest:

$input = 'foo_left.jpg';
if(!preg_match('/_(left|right|center)/', $input, $matches)) {
    // no match
}

$pos = $matches[0]; // "_left", "_right" or "_center"

See it in action.

Update:

For a more defensive-minded approach (if there might be multiple instances of "_left" and friends in the filename), you can consider adding to the regex.

This will match only if the l/r/c is followed by a dot:

preg_match('/(_(left|right|center))\./', $input, $matches);

This will match only if the l/r/c is followed by the last dot in the filename (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 using these regexes, you will find the result in $matches[1] instead of $matches[0].

Sign up to request clarification or add additional context in comments.

6 Comments

This won't work if _left|right|center happens to be in the middle of the string also
@Czechnology, It would match _left_blahblahblah.jpg. Not sure if thats legal.
@DougT.: Unless we can make some assumptions about the file name, life will be really hard. Think about _left.jpg.I can have many 'extensions' if I want to.png_center.png.
PS: You don't need to escape the dot in character groups, you can just write [^.]. Furthermore I would use a + there instead of an * in order to ensure that there is some file extension.
Actually, $matches[0] will contain the full string. You want $matches[1] which'll have the first capture group in it.
|
1

Someone will probably have more elegant solution but I would use strpos like you suggest.

if (strpos(strtoupper($img), '_LEFT') > 0) $pos = '_LEFT'

you could then expand it to

$needleArray = arra('_LEFT', '_CENTER', '_RIGHT');
foreach ($needleArray as $needle) {
  if (strpos(strtoupper($img), $needle) > 0) $pos = $needle
}

This is assuming you can't have more than one value for pos.

Comments

1

Non-regex option

$img = 'myimage_left.jpg';
$find = array( 'left', 'center', 'right' );

if ( in_array( $pos = end( explode( '_',  basename( $img, '.jpg' ) ) ), $find ) ) {
    echo $pos;
}

Comments

0

If you want to avoid regexes, you could just do

  $underScores = explode("_", $img);
  $endOfFileName = end($underScores);
  $withoutExtensionArr = explode(".", $img);
  $leftRightOrCenter = $withoutExtensionArr[0];

Then check the contents of left right or center

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.