Viewed   66 times

PHP is telling me that split is deprecated, what's the alternative method I should use?

 Answers

5

explode is an alternative. However, if you meant to split through a regular expression, the alternative is preg_split instead.

Monday, September 5, 2022
2

Assuming you are using UTF-8 (or you can convert it to UTF-8 using Iconv or some other tools), then using the u modifier (doc: http://www.php.net/manual/en/reference.pcre.pattern.modifiers.php )

<?
    $s = "FrontPage 2000???????";
    print_r(preg_match_all('/./u', $s, $matches));
    echo "n";
    print_r($matches);
?>

will give

21
Array
(
    [0] => Array
        (
            [0] => F
            [1] => r
            [2] => o
            [3] => n
            [4] => t
            [5] => P
            [6] => a
            [7] => g
            [8] => e
            [9] =>  
            [10] => 2
            [11] => 0
            [12] => 0
            [13] => 0
            [14] => ?
            [15] => ?
            [16] => ?
            [17] => ?
            [18] => ?
            [19] => ?
            [20] => ?
        )

)

Note that my source code is stored in a file encoded in UTF-8 also, for the $s to contain those characters.

The following will match alphanumeric as a group:

<?
$s = "FrontPage 2000???????";
print_r(preg_match_all('/(w+)|(.)/u', $s, $matches));
echo "n";
print_r($matches[0]);
?>

result:

10
Array
(
    [0] => FrontPage
    [1] =>  
    [2] => 2000
    [3] => ?
    [4] => ?
    [5] => ?
    [6] => ?
    [7] => ?
    [8] => ?
    [9] => ?
)
Saturday, September 17, 2022
5

Here's what worked best for me when trying to script this (in case anyone else comes across this like I did):

$ pecl -d php_suffix=5.6 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.0 install <package>
$ pecl uninstall -r <package>

$ pecl -d php_suffix=7.1 install <package>
$ pecl uninstall -r <package>

The -d php_suffix=<version> piece allows you to set config values at run time vs pre-setting them with pecl config-set. The uninstall -r bit does not actually uninstall it (from the docs):

vagrant@homestead:~$ pecl help uninstall
pecl uninstall [options] [channel/]<package> ...
Uninstalls one or more PEAR packages.  More than one package may be
specified at once.  Prefix with channel name to uninstall from a
channel not in your default channel (pecl.php.net)

Options:
  ...
  -r, --register-only
        do not remove files, only register the packages as not installed
  ...

The uninstall line is necessary otherwise installing it will remove any previously installed version, even if it was for a different PHP version (ex: Installing an extension for PHP 7.0 would remove the 5.6 version if the package was still registered as installed).

Monday, December 12, 2022
4

preg_split() is similar to the old ereg-function split(). You only have to enclose the regex in /.../ like so:

preg_split('/www/', 'D:/Projects/job.com/www/www/path/source', 2);

The enclosing slashes / here are really part of the regular expression syntax, not searched for in the string. If the www delimiter is variable, you should additionally use preg_quote() for the inner part.

But note that you don't need regular expressions if you only look for static strings anyway. In such cases you can use explode() pretty much like you used split() before:

explode('www', 'D:/Projects/job.com/www/www/path/source', 2);
Thursday, November 24, 2022
 
4

Never used any of those, but they look interesting..

Take a look at Gearman as well.. more overhead in systems like these but you get other cool stuff :) Guess it depends on your needs ..

Friday, November 11, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :