function split() deprecated - besides preg_split what else needs to be changed in this code? general idea [closed]

StackOverflow https://stackoverflow.com/questions/23368834

  •  12-07-2023
  •  | 
  •  

Pergunta

I know that there are several questions about this issue, but this is a particullar case... In the following code (in the the first and the last line) I have to replace split with preg_split, but I think something else needs to be changed too.

Please tell me what should I change in the code for it to work, and the theory behind this change, i.e. the gereral idea behind switching between split and preg_split. The code which needs the transition is:

            $opt = split("_",$key);
            if($opt[0]=="id" && $val!="0" && $val!=""){

                        some queries

            $shuffle=split("_",$_POST['all_'.$i]);
Foi útil?

Solução

Use explode instead of split. Your code should look like this :

$opt = explode("_",$key);
if($opt[0]=="id" && $val!="0" && $val!=""){

   some queries

$shuffle=explode("_",$_POST['all_'.$i]);

Documentation : http://fr2.php.net/explode

Outras dicas

PHP is in the process of dropping an older POSIX-compatible regex extension in favour of the newer PCRE extension. This means that older functions like split() and ereg() will be removed in time.

The PCRE equivalent for split() is preg_split(), which has a modified syntax. For your code you'd use:

$opt = preg_split("/_/",$key);

However, a Regex function is a heavyweight tool and isn't required here. You just need explode(), like this:

$opt = explode("_",$key);
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top