如何将 split() 转换为 preg_split()?
我想将以下 split 函数转换为 preg_split ..这有点令人困惑,因为该值会不时改变...
当前代码:
$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 函数的输出:
Array ( [0] => D:/Projects/job.com/ [1] => /www/path/source )
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 )
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
preg_split() 与旧的 ereg 函数 split() 类似。您只需将正则表达式括在
/.../
中,如下所示:这里包含的斜杠
/
实际上是正则表达式语法的一部分,而不是在字符串中搜索。如果www
分隔符是可变的,您还应该对内部部分使用 preg_quote() 。但请注意,如果您只查找静态字符串,则不需要正则表达式。在这种情况下,您可以像之前使用 split() 一样使用
explode()
:preg_split() is similar to the old ereg-function split(). You only have to enclose the regex in
/.../
like so:The enclosing slashes
/
here are really part of the regular expression syntax, not searched for in the string. If thewww
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: