4

I have a string like abcdefg123hijklm. I also have an array which contains several strings like [456, 123, 789].

I want to check if the number in the middle of abcdefg123hijklm exists in the array.

How can I do that? I guess in_array() won't work.

1
  • This is a poorly expressed minimal reproducible example because we don't know if abc4567def should be matched or not. Do full integers need to be matched? This page is probably not suitable for closing future duplicates. Commented Nov 28, 2022 at 3:36

4 Answers 4

16

So you want to check if any substring of that particular string (lets call it $searchstring) is in the array? If so you will need to iterate over the array and check for the substring:

foreach($array as $string)
{
  if(strpos($searchstring, $string) !== false) 
  {
    echo 'yes its in here';
    break;
  }
}

See: http://php.net/manual/en/function.strpos.php

If you want to check if a particular part of the String is in the array you will need to use substr() to separate that part of the string and then use in_array() to find it.

http://php.net/manual/en/function.substr.php

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

1 Comment

I would put the echo part in a code block (curly brackets) and add a break; right before the closing curly bracket - helps in performance when you just need to know if at least one string matches.
8

Another option would be to use regular expressions and implode, like so:

if (preg_match('/'.implode('|', $array).'/', $searchstring, $matches))
    echo("Yes, the string '{$matches[0]}' was found in the search string.");
else
    echo("None of the strings in the array were found in the search string.");

It's a bit less code, and I would expect it to be more efficient for large search strings or arrays, since the search string will only have to be parsed once, rather than once for every element of the array. (Although you do add the overhead of the implode.)

The one downside is that it doesn't return the array index of the matching string, so the loop might be a better option if you need that. However, you could also find it with the code above followed by

$match_index = array_search($matches[0], $array);

Edit: Note that this assumes you know your strings aren't going to contain regular expression special characters. For purely alphanumeric strings like your examples that will be true, but if you're going to have more complex strings you would have to escape them first. In that case the other solution using a loop would probably be simpler.

Comments

2

You can do it reversely. Assume your string is $string and array is $array.

foreach ($array as $value)
{
    // strpos can return 0 as a first matched position, 0 == false but !== false
    if (strpos($string, $value) !== false)
    {
        echo 'Matched value is ' . $value;
    }  
} 

1 Comment

that wouldnt always work because if the $value is found at the start of $str strpos() will return 0 which evaluates as false
1

Use this to get your numbers

$re = "/(\d+)/";
$str = "abcdefg123hijklm";

preg_match($re, $str, $matches);

and ( 123 can be $matches[1] from above ):

   preg_grep('/123/', $array);

http://www.php.net/manual/en/function.preg-grep.php

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.