Home >Backend Development >PHP Tutorial >PHP 分割字串的问题

PHP 分割字串的问题

WBOY
WBOYOriginal
2016-06-06 20:38:151122browse

在php中,如果我要把e前和后的字串分割该怎么办?

像这样:
abcde1234 转成 abcd 1234
小白求助...

回复内容:

在php中,如果我要把e前和后的字串分割该怎么办?

像这样:
abcde1234 转成 abcd 1234
小白求助...

简单 正则搞定

$str = "abcde1234";
preg_match('/^(\w+)e(\d+)$/i',$str,$matches);

print_r ($matches);

Array
(
[0] => abcde1234
[1] => abcd
[2] => 1234
)

explode()函数 http://www.w3school.com.cn/php/func_string_explode.asp

<code><?php $str = "abcde1234";
print_r (explode("e",$str));

Array (
    [0] => abcd
    [1] => 1234
)
</code>

在线演示:http://3v4l.org/k7Dap

支持楼上,当然你也可以用strpos查找位置,然后用substr来裁剪出来。具体用法可以看手册。看你具体怎么想了

正则表达式也可以做到更复杂的分割

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn