-2

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]);
Klauss Gekker
  • 143
  • 2
  • 8
  • Can you post your input and expected output? – Halcyon Apr 29 '14 at 15:23
  • 4
    Use `explode` instead of split. – PoulsQ Apr 29 '14 at 15:27
  • Yes to @PoulsQ. You are not using `preg_split`. – AbraCadaver Apr 29 '14 at 15:32
  • I have tried using preg_split instead of split, and I have got the errors "no ending delimiter '_' found on that line ..." – Klauss Gekker Apr 29 '14 at 15:34
  • @KlaussGekker: You don't need `preg_split()`. **Use `explode()`.** – Amal Murali Apr 29 '14 at 15:36
  • Thanks everybody! I have replaced split with explode and no error appears. everything is working okay. Good luck and thank you @PoulsQ ! – Klauss Gekker Apr 29 '14 at 15:39
  • @KlaussGekker: I downvoted the question not only because it looks like you just didn't want to read [the manual](http://www.php.net/manual/en/function.split.php) (*Tip: split() is deprecated as of PHP 5.3.0. preg_split() is the suggested alternative to this function. If you don't require the power of regular expressions, it is faster to use explode(), which doesn't incur the overhead of the regular expression engine.*) but also because of "this is a particular case". Does that mean *your* particular case is better than the others? I would downvote again if I could. – Jon Apr 29 '14 at 15:43
  • @Jon, It is not better better, it is just different... you see, I have read other cases where explode was not apropriate to use instead of split, because it can be used with just one delimiter... but in this particular case, explode works perfectly... anyways... thanks for your constructive critique and I wish you good luck – Klauss Gekker Apr 29 '14 at 15:53

2 Answers2

1

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

PoulsQ
  • 1,936
  • 1
  • 15
  • 22
1

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);