0

I want to be able to trim a string where values match an array like this

$image_formats = array('.png','.jpg', '.jpeg', '.gif');
$file = 'image1.png';

$file_stripped = trim($file, $image_formats);

Wanted Result: 'image1'

Is there a function for this, whats the best method to go about achieving this?

1

2 Answers 2

3

You can use str_replace passing an array of search values:

$image_formats = array('.png','.jpg', '.jpeg', '.gif');
$file = 'image1.png';

$file_stripped = str_replace($image_formats, '', $file);
Sign up to request clarification or add additional context in comments.

Comments

1

trim() is for removing matches of individual characters, not longer strings.

You can convert $image_formats to a regular expression and use preg_replace().

$image_formats = '/\.(png|jpg|jpeg|gif)$/';
$file_stripped = preg_replace($image_formats, '', $file);

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.