首页 > PHP 分割字串的问题

PHP 分割字串的问题

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

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


简单 正则搞定

<?php

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

print_r ($matches);

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


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


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

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

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

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


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

【热门文章】
【热门文章】