Good Day, I have a question:

There is a line for example:

abracadabra-cudsa_123-qwerty-text_a-abraca-trollololo-nlo

If the line has a word (marker) = text_a then output: Привет Васа!

If there is a word in the line (marker) = text_b then output: Привет Лана!

If the string is not found text_a or text_b then text_b default: Привет Мир!

I do this:

 $text = $_GET['text']; $main_str = $text; //искомый текст $my_str = 'text_a'; $pos = strpos($main_str, $my_str); if ($pos === false) { echo 'Привет Мир!'; }else{ echo 'Привет Васа!'; } 

But here the problem is that looking for only one word (marker) Help solve! Thank!

    3 answers 3

    As an option:

     $text = $_GET['text']; $main_str = $text; if (strpos($main_str, 'text_a') !== false) { echo 'Привет Васа!'; } elseif (strpos($main_str, 'text_b') !== false) { echo 'Привет Лана!'; } else { echo 'Привет Мир!'; } 

    PS And what should I do if both are found?

       $text = $_GET['text']; $main_str = $text; $has_a = strpos($main_str, 'text_a') !== false; $has_b = strpos($main_str, 'text_b') !== false; if ($has_a && $has_b) { echo 'ОШИБКА'; } else { if ($has_a) { echo 'А'; } elseif ($has_b) { echo 'Б'; } else { echo 'ПУСТО'; } } 

        since it's worth the регулярные-выражения label

         <?php $main_str = 'abracadabra-cudsa_123-qwerty-text_a-abraca-trollololo-nlo'; //искомый текст $my_str = '/text_a|text_b/'; $matches = array(); preg_match_all($my_str, $main_str, $matches); if (array_search('text_a', $matches[0]) !== false) echo 'Привет, Васа!'; elseif(array_search('text_b', $matches[0]) !== false) echo 'Привет, Лана!'; else echo 'Привет, Мир!'; 
        • one
          put a mark on regular-expressions because I thought that through regular-expressions it would be easier to do this, but then I read the forums, they say that using regular-expressions slows down PHP. - John Freeman
        • everything is relative, sometimes it is faster to write a regular list with a certain syntax and behavior than to search manually using php. A regular is almost always better - Vasily Barbashev
        • @ Vasily Barbashev: I do not agree with you. Regularity is almost always better where it is needed - yes. Where there is no need, it's better to get by with simple string functions. More difficult from this will not work. You forget that PHP is written in C. And in C, a string is actually a very complex structure of char sequences. Although from the height of php it seems to us that we work with strings as well as, for example, with numbers. - user200141
        • I’m a little bit about this, it’s clear that from a string of 5 characters, it’s much easier to find out if a character is there using a special method in php than in a regular one, but for more complex parsing (not just finding a string in a substring), the regular isn’t bypass, and it is actually not so slow, otherwise an alternative method of processing would have been devised) - Vasily Barbashev
        • @ Vasily Barbashev: Apparently you misunderstood you too. I do not urge not to use regulars at all, I urge not to use regulars where you can work with ordinary string functions. For this, I think not to discuss further, since we are talking about different things) - user200141