-4

I have a string like this: 12PUM4

It has two to three numbers at the beginning, it has three characters in the middle and it has one to two numbers at the end.

I want to split it somehow into three section:

  1. the numbers until the characters
  2. the characters from the middle
  3. the rest behind the characters.

Can someone help please?

Michael Irigoyen
  • 22,513
  • 17
  • 89
  • 131
user1185551
  • 217
  • 1
  • 2
  • 12
  • you need to use `explode` with some `regex` http://php.net/manual/en/function.preg-split.php – M Khalid Junaid May 21 '13 at 11:51
  • The problem is I haven't used regex before, so I thought some could help in more depth. – user1185551 May 21 '13 at 11:54
  • Links http://stackoverflow.com/questions/6278296/extract-numbers-from-a-string, http://stackoverflow.com/questions/4837278/filter-out-numbers-in-a-string-in-php – zamil May 21 '13 at 11:59
  • No need to use explode. You cant use explode without a delimiter. No need to use regex either, as they are complex :P . Look at my answer, the easy way to do it ;) – Altaf Hussain May 21 '13 at 12:10

5 Answers5

4

You can use preg_match();

$str = '12PUM4';
$matches = array();
preg_match('/([0-9]+)([a-zA-z]+)(.*)/', $str, $matches);
print_r($matches);

Output

Array ( [0] => 12PUM4 [1] => 12 [2] => PUM [3] => 4 )

when you use this function it will split text and put matches in $matches array

  • [0-9]+ - matches for numbers at least one or more
  • [a-zA-Z]+ are characters at least one or more
  • .* is anything(almost)
  • () - is used as subpattern which is put into $matches array

More information of how to use preg_match can be found here

The solution with substr() under condtion you wrote that it has 12 digits, 3 characters and 1 or 2 digits in the end.

$str = '12PUM4';
$matches = array( 0 => substr($str,0, 2), 1 => substr($str, 2, 3) , 2 => substr($str, 5, strlen($str)>6 ? 2 : 1));
print_r($matches);

Output

Array ( [0] => 12 [1] => PUM [2] => 4 )
Robert
  • 19,800
  • 5
  • 55
  • 85
1

sscanf() would also be an option here:

$input = '12PUM4'; 
$splitValues = sscanf('%d%[A-Z]%d', $input); 
var_dump($splitValues);
Mark Baker
  • 209,507
  • 32
  • 346
  • 385
0

Use the regular expression

$string = '12PUM4';
$array = preg_split('#(?<=\D)(?=\d)|(?<=\d)(?=\D)#i', $string);
Nagarjun
  • 2,346
  • 19
  • 28
0

try this

$name = "12PUM4"
$pattern = "/^([0-9]{2,3})([A-Z]{3})([0-9]{1,2})$/";
preg_match($pattern, $name , $matches);
echo $matches[1]; // first numbers
echo $matches[2]; // middle chars
echo $matches[3]; // last numbers
rcpayan
  • 535
  • 3
  • 15
  • It will show either 2,3 numbers 3 characters and 1 or 2 numbers at end. Read the question because he asked for example "the numbers until the characters" – Robert May 21 '13 at 12:04
  • `It has two to three numbers at the beginning, it has three characters in the middle and it has one to two numbers at the end.` did you read that part of question? btw thx for your `not useful` vote :) – rcpayan May 21 '13 at 12:06
  • Okay you're right but he also wrote "the numbers until the characters the characters from the middle the rest behind the characters." anyway I'll cancel downvote. – Robert May 21 '13 at 12:07
  • it's ok and my solution is right, its output : `Array ( [0] => 12PUM4 [1] => 12 [2] => PUM [3] => 4 )`, whats wrong with that? – rcpayan May 21 '13 at 12:09
  • small chars are easy part of the question, just add `i` at the end of the pattern :) – rcpayan May 21 '13 at 12:51
0
$string = '12PUM4';

$strCnt = strlen($string);
$tmpStr = '';
$conArr = array();
for($i=0;$i<$strCnt;$i++){
    if(is_numeric($string[$i])){
        if($tmpStr != '' && !is_numeric($tmpStr)){
            $conArr[] = $tmpStr;
            $tmpStr = '';
        }
        $tmpStr .= $string[$i]; 
    }else{
        if(is_numeric($tmpStr)){
            $conArr[] = $tmpStr;
            $tmpStr = '';
        }
        $tmpStr .= $string[$i]; 
    }
}
$conArr[] = $tmpStr;

print_r($conArr);
Chetan
  • 21
  • 1