0

I am beginner in php.

I have this code:

function allergen(string $value): string
    {
        $allergens = ['pszenica', 'pszenicy', 'żyto', 'żyta', 'jęczmień', 'jęczmieniem', 'jęczmienia', 'owies', 'owsem', 'owsa', 'orkisz', 'orkiszem', 'orkisza', 'jaja', 'jajka', 'jajo', 'jajecznica', 'jajeczny', 'jajeczna', 'z jajkiem', 'jajko', 'ryby', 'ryb', 'rybne', 'rybny', 'dorsz', 'dorsza', 'dorszem', 'łosoś', 'łososiem', 'łososia', 'mintaj', 'mintajem', 'mintaja', 'makrela', 'makrelą', 'makreli', 'miruna', 'miruną', 'miruny', 'pstrąg', 'pstrągiem', 'pstrąga', 'sola', 'śledź', 'śledziem', 'śledzi', 'sardynki', 'sardynka', 'sardynek', 'sardynkami', 'halibut', 'halibuta', 'halibutem', 'szczupak', 'szczupakiem', 'szczupaka', 'sandacz', 'sandaczem', 'sandacza', 'tuńczyk', 'tuńczyka', 'tuńczykiem', 'kergulena', 'kergulą', 'orzeszki', 'orzechy', 'orzech', 'orzechowy', 'orzechowe', 'orzechami', 'migdały', 'migdałami', 'migdał', 'nerkowce', 'pistacje', 'pistacjami', 'pistacji', 'migdałowe', 'płatki migdałowe', 'płatkami migdałowymi', 'seler', 'z selerem', 'selerowy', 'gorczyca', 'gorczycą', 'gorczycy', 'sezam', 'sezamowe', 'sezamu', 'sezamem', 'musztarda', 'musztardą'];
        if (in_array(mb_strtolower($value), $allergens)) {
            return "<b>$value</b>";
        } else {
            return $value;
        }
    }

It's working fine when I use this function:

allergen('owies');

(add <b>owies</b>)

But problem is when I have:

allergen('lubie takie owies')

I need result:

lubie takie <b>owies</b>

How repair this function?

1
  • “But problem is when I have:” - and that problem is what, actually? Please explain what the problem is when asking a question here, instead of just stating that there is one! Commented Jan 15, 2020 at 14:51

2 Answers 2

2

An easy way for your example to work is to create an array of all the words in your input, and check each of them separately. Here is an example:

function allergen(string $value): string
    {
        $return = "";
        $allergens = ['pszenica', 'pszenicy', 'żyto', 'żyta', 'jęczmień', 'jęczmieniem', 'jęczmienia', 'owies', 'owsem', 'owsa', 'orkisz', 'orkiszem', 'orkisza', 'jaja', 'jajka', 'jajo', 'jajecznica', 'jajeczny', 'jajeczna', 'z jajkiem', 'jajko', 'ryby', 'ryb', 'rybne', 'rybny', 'dorsz', 'dorsza', 'dorszem', 'łosoś', 'łososiem', 'łososia', 'mintaj', 'mintajem', 'mintaja', 'makrela', 'makrelą', 'makreli', 'miruna', 'miruną', 'miruny', 'pstrąg', 'pstrągiem', 'pstrąga', 'sola', 'śledź', 'śledziem', 'śledzi', 'sardynki', 'sardynka', 'sardynek', 'sardynkami', 'halibut', 'halibuta', 'halibutem', 'szczupak', 'szczupakiem', 'szczupaka', 'sandacz', 'sandaczem', 'sandacza', 'tuńczyk', 'tuńczyka', 'tuńczykiem', 'kergulena', 'kergulą', 'orzeszki', 'orzechy', 'orzech', 'orzechowy', 'orzechowe', 'orzechami', 'migdały', 'migdałami', 'migdał', 'nerkowce', 'pistacje', 'pistacjami', 'pistacji', 'migdałowe', 'płatki migdałowe', 'płatkami migdałowymi', 'seler', 'z selerem', 'selerowy', 'gorczyca', 'gorczycą', 'gorczycy', 'sezam', 'sezamowe', 'sezamu', 'sezamem', 'musztarda', 'musztardą'];
        $words = explode(' ', $value); // create an array of words
        foreach($words as $word) { //iterate through words
            if (in_array(mb_strtolower($word), $allergens)) {
                $return .= "<b>" . $word . "</b> ";
            } else {
                $return .= $word . " ";
            }
        }
        return trim($return); //remove trailing space from the end
    }

    echo allergen('lubie takie owies'); // returns: lubie takie <b>owies</b>
Sign up to request clarification or add additional context in comments.

Comments

1

This can be done with preg_replace() instead of in_array(). Note, however, that you'll need to add delimiters to each string you in your $allergens array.

$string = 'lubie takie owies';
$allergens = ['/pszenica/', '/pszenicy/', '/żyto/', '/żyta/', '/jęczmień/', '/jęczmieniem/', '/jęczmienia/', '/owies/', '/owsem/', '/owsa/'];

$result = preg_replace($allergens, '<b>$0</b>', $string);

var_dump($result);

DEMO

2 Comments

This is a lot more elegant solution than my answer. The sole reason I didn't bring it up is that I have mild PTSD when using regular expressions, and wouldn't want to recommend it to a newcomer either. Irregardless, this should be accepted.
@Andrew There's a time and a place for everything. Regex isn't always the most appropriate solution, even if it does solve the problem, and even if it appears to be the most "elegant". There's overhead that comes into play when using regex functions that doesn't exist with most of the basic string-comparison functions. In this case, I'm not really sure which route is the most efficient. I'll leave it as an exercise for the OP to run benchmark tests if they want.

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.