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
  •  | 
  •  

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]);
有帮助吗?

解决方案

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

其他提示

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);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top