0

I'm developing an application that takes a full name of a person and then processes it. For example, if the user enters the name like this:

"AlanMichel"

Then the result must be:

"Alan Michel"

I didn't know how I can do that in the php. Anyone can help please?

SherylHohman
  • 16,580
  • 17
  • 88
  • 94

2 Answers2

2

You can do:

$str = "AlanMichel";
$name = preg_split('/(?=[A-Z])/',$str);

echo implode( " ", $name );

This will result to:

Alan Michel 
Eddie
  • 26,593
  • 6
  • 36
  • 58
0

Try this

function splitAtUpperCase($s) {
        return preg_split('/(?=[A-Z])/', $s, -1, PREG_SPLIT_NO_EMPTY);
}

$str = "AlanMichel";
$strArray = splitAtUpperCase($str));
echo $strArray[0]; //first name
echo $strArray[1]; //second name

Output

Alan
Michel

If you don't need the array itself, you can just preprend uppercase characters (except the first) with a space

echo  preg_replace('/(?<!^)([A-Z])/', ' \\1', $str); 
shubham715
  • 3,324
  • 1
  • 17
  • 27