开发者

How do I add white space between word

开发者 https://www.devze.com 2023-02-04 04:38 出处:网络
I want to add space to word something like this CountryName RegionName ZipPostalCode to be Country Name Region N开发者_StackOverflow中文版ame

I want to add space to word something like this

CountryName
RegionName
ZipPostalCode

to be

Country Name
Region N开发者_StackOverflow中文版ame
Zip Postal Code

Let me know how can be done with php


You can use regular expressions to find [lowercase character][uppercase character] and insert a space:

$newstr = preg_replace('/([a-z])([A-Z])/s','$1 $2', $oldstr);


You might look into CakePHP's Inflector class for guidance (for example the humanize function).


Are they all camelCase like that? You can turn it into an array, then turn that into a string.

<?php
function splitCamelCase($str) {
  return preg_split('/(?<=\\w)(?=[A-Z])/', $str);
}

print_r(splitCamelCase("ZipPostalCode"));
?>

Edit: Disregard this - Mark's answer is better.


$new = preg_replace('/([a-z])([A-Z])/', '$1 $2', $old);


Use preg_replace()

$str = 'HelloThere';
$str= preg_replace('/(?<=\\w)(?=[A-Z])/'," $1", $str);
echo trim($str); //Hello There


<?php
//  It can be done as:
echo 'Country ','Name <br>';
echo 'Region ','Name <br>';
echo 'Zip ','Postal ','Code';

//   OR

echo 'Country ','Name <br> Region ','Name <br> Zip ','Postal ','Code';

?>
0

精彩评论

暂无评论...
验证码 换一张
取 消