C
C
Cheizer2018-04-10 20:38:28
PHP
Cheizer, 2018-04-10 20:38:28

How to make a comparison and find a word in a string?

Friends, PHP gurus. There is such a list
CLIENTS==clients||
SERVICES==service||
CONTACTS==contacts
From it I get only the second part of the line, for example service

//что имеем на входе
<?php
$snippet = "service";  // часть строки из полного списка
$inputoptions = "КЛИЕНТЫ==clients||УСЛУГИ==service||КОНТАКТЫ==contacts";  //полный список
 
$snippets = explode('||',$inputoptions);

foreach ($snippets as $snippetoption){
    $snippetoption_parts = explode('==',$snippetoption);
    $value = isset($snippetoption_parts[0]) ? $snippetoption_parts[0] : '';     
    $text = isset($snippetoption_parts[1]) ? $snippetoption_parts[1] : $value;
    if ($snippet == $value){
        return $text;
    }
}
return '';

At the output, I want to get the word SERVICES, but it doesn’t work, what am I doing wrong? :(

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
Lazy @BojackHorseman PHP, 2018-04-10
@Cheizer

you are looking in the wrong index

if ($snippet == $text) {
    return $value;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question