0

How can I detect if a string ends with values inside an array?

array('.1.jpg','.2.jpg','.3.jpg','.4.jpg','.5.jpg','.6.jpg','.7.jpg','.8.jpg','.9.jpg');

the string can be: image.1.jpg or it can be image.11.jpg

But I need to find only if string ends with one the values inside the array.

7
  • 1
    preg_match('/\.[1-9]\.jpg$/', $string) Commented Dec 20, 2019 at 17:29
  • @AlexHowansky Simple and effective. Thanks! Commented Dec 20, 2019 at 17:56
  • @Alex please do not deliberately ruin site design by providing solutions as comments. This leads to question abandonment and volunteers who answer are not rewarded by the OP. Commented Dec 22, 2019 at 2:12
  • @bpy are your requirements literal/rigid that the numeric values are ALWAYS single digit or might the list of strings contain two consecutive digits? What does your realistic data look like? You should probably also work on "answer acceptance rate". A quick glance at the questions in your profile reveals that not too many of your wuestion are marked as resolved -- this is not great for the volunteers who give you their time and insights. Commented Dec 22, 2019 at 19:18
  • Did you give up??? Commented Jan 28, 2020 at 20:04

4 Answers 4

2

If you need to use all periods in the string, then just get everything from the first period to the end and see if it is in the array:

if(in_array(strstr($string, '.'), $array)) {
    //yes
}

Obviously something like my.image.1.jpg would fail as it would look for .image.1.jpg.

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

Comments

1

A good-old solution, which gives you as well the found index in the array:

function endsBy(string $str, array $arr): ?int
{
    $len = strlen($str);
    foreach ($arr as $i => $toTest) {
        $toTestLen = strlen($toTest);
        if ($toTestLen <= $len && substr_compare($str, $toTest, $len - $toTestLen) === 0) {
            return $i;
        }
    }

    return null;
}

To use it as described in your question:

if (endsBy($myString, $array) !== null) {
    ...
}

Comments

0
if (in_array(substr($string, -6), $array)) {
    //yes
}

As all your examples have the exact same length. No REGEX or replacement required.

3 Comments

OP said it can be that, not that it would match. It is not in the array.
The magic -6 is the part that breaks this answer (makes it incorrect/unsuitable).
@mickmackusa: Could you explain that notion?
0

I had a similar situation. In my case, I wanted to test if a filename ended with one of the given extensions. The allowedExtensions could be passed into a function or defined as a constant. It may also change in the future.

I went with something like this:

const allowedExtensions = ['.docx', '.jpg', '.txt', '.csv'];
const isAllowed = allowedExtensions.some((ext) => filename.endsWith(ext));

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.