A
A
Anastasia2021-07-06 03:46:21
PHP
Anastasia, 2021-07-06 03:46:21

Is it possible to immediately assign a variable in one action, get [0] from it and compare?

Hello. I need to make a condition under which I check if the line starts with the word "Delete" and if so, I process the second part of the code.

$msg = 'Удалить Александр Петров';
if(mb_strtolower(explode(' ',trim($msg), 2)[0] == 'удалить') {
  // здесь нужно работать c "Александр Петров"
}


If I write $perem = explode... before explode, then it will be equal to the word Delete , because then I call [0]. Tell me, except for calling explode again - are there any solutions?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Rsa97, 2021-07-06
@nastya97core

$msg = 'Удалить Александр Петров';
$command = explode(' ', trim($msg), 2);
if(mb_strtolower($command[0]) === 'удалить') {
  // здесь нужно работать c $command[1]
}

J
John Didact, 2021-07-06
@JohnDidact

If I felt sorry for using regular expressions, I would use mb_strlen()- to count the number of characters in the 'Delete' string (well, or not count, but write it myself). Then mb_stripos()to check if the line contains the desired line (Delete) and whether it is at the beginning. Then, mb_substr()in order to pull out the "second part of the code".
PS Online or go to the registry office?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question