开发者

PHP split to preg_split()

开发者 https://www.devze.com 2023-02-05 17:40 出处:网络
I wanted to convert the following split function, which I have been using to preg_split.. it\'s a little confusing, because the value will change from time to time...

I wanted to convert the following split function, which I have been using to preg_split.. it's 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);

Output of the split function:

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


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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号