-10

I need to slice a string into 2 parts, such that, for example, if the string is "4s5ee9f8fg", I need it as "4598 seeffg"

I'm trying with this:

$string2 = '132xx';
preg_match("/[0-9]+/",trim($string2),$result);
echo $result[0];
echo $result[1];

Here I'm getting just numeric characters, but not alphabetic characters.

Can anyone give a solution?

4

1 回答 1

0

You're on the right track with what you have.

You are currently only trying to get the numeric characters, which is why that's all you are getting. Also, after some testing with different strings, it turned out that your particular regex seems to only works when all the numbers are next to each other. I figured out a way to do this with preg_replace instead of preg_match.

Try this:

$string2 = '4s5ee9f8fg';
$result1 = preg_replace("/[A-z]+/", "", trim($string2));
$result2 = preg_replace("/[0-9]+/", "", trim($string2));
$finalResult = $result1." ".$result2;
echo $finalResult."\n";
于 2013-06-28T14:18:23.277 回答