1

I've got an array of values, all containing one word, and I'd like to be able to find which of those values is found first in a string.

$materials = array("cotton","silk","polyester","denim","wool");
$string1 = "The fabric composition of this jacket is 100% cotton (body) and 65% polyester / 35% cotton (lining)";
$string2 = "The jeans are made from denim with cotton pockets";

So for the $string1, I'd like it to say that it found 'cotton' first as the material and for the $string2 I'd like it to say that it found 'denim' first.

Do you know a way of doing this? I was originally looking at a foreach loop but it would go in order of the array meaning it would also bring 'cotton' back for both strings as that's the first one in the array:

foreach ($materials as $material) {
    if (stripos($string1, $material) !== FALSE) {
        $product_material1 = $material;
        break;
    }
}
3
  • 3
    Do you know a way of doing this? Did you tried something? If yes show us your attempt(s), so we can show you what you did wrong and where you can improve your code Commented Mar 6, 2015 at 15:50
  • split the strings into arrays using a space as a delimiter. Cross check your new array values with $materials. If they match do something. Commented Mar 6, 2015 at 15:54
  • What's stopping you from collecting starting indexes of elements from $materials in $string1 and $string2, then just selecting the smallest? Commented Mar 6, 2015 at 15:56

2 Answers 2

2
$materials = array("cotton","silk","polyester","denim","wool");
$string1 = "The fabric composition of this jacket is 100% cotton (body) and 65% polyester / 35% cotton (lining)";
$string2 = "The jeans are made from denim with cotton pockets";

$firstMatch = array_shift(array_intersect(str_word_count($string1, 1), $materials));
var_dump($firstMatch);

$firstMatch = array_shift(array_intersect(str_word_count($string2, 1), $materials));
var_dump($firstMatch);

If there's no match, you'll get a null

Note that it's case-sensitive

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

1 Comment

I changed it so that the $string1 was all lower case so the case sensitive bit wasn't an issue: $firstMatch = array_shift(array_intersect(str_word_count($string1, 1), $materials)); I'm going to mark this as the answer as it's done exactly what I asked for thank you!!!
1

I like Mark Baker's solution because I like one liners, but here is another solution with regular expressions and a helper function.

function findFirst($haystack, $needles) {
    if (preg_match('/'.implode('|', $needles).'/', $haystack, $matches)) {
        return $matches[0];
    }

    return null;
}

$first1 = findFirst($string1, $materials);
var_dump($first1);

$first2 = findFirst($string2, $materials);
var_dump($first2);

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.