PHP split into preg_split ()

I wanted to convert the following separation function that I used into preg_split .. this is a little confusing because the value will change from time to time ...

Current Code:

$root_dir = 'www'; $current_dir = 'D:/Projects/job.com/www/www/path/source'; $array = split('www', 'D:/Projects/job.com/www/www/path/source', 2); print_r($array); 

Split Function Output:

 Array ( [0] => D:/Projects/job.com/ [1] => /www/path/source ) 
+9
source share
1 answer

preg_split () is similar to the old split () ereg function. You should only add the regex to /.../ as follows:

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

Nested slashes / here are indeed part of the regular expression syntax that does not execute in a string. If the www delimiter is a variable, you must additionally use preg_quote () for the inside.

But note that you don't need regular expressions if you look at static strings anyway. In such cases, you can use explode() in much the same way you used split ():

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

All Articles