How to add a space between words

I want to add a space to say something like this

CountryName RegionName ZipPostalCode 

be

 Country Name Region Name Zip Postal Code 

Tell me how to do this with php

+4
source share
6 answers

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

 $newstr = preg_replace('/([az])([AZ])/s','$1 $2', $oldstr); 
+12
source

You can take a look at the CakePHP Inflector class for guidance (e.g. humanization function).

+1
source

Use preg_replace()

 $str = 'HelloThere'; $str= preg_replace('/(?<=\\w)(?=[AZ])/'," $1", $str); echo trim($str); //Hello There 
0
source

Do they all look like camelCase? You can turn it into an array and then turn it into a string.

 <?php function splitCamelCase($str) { return preg_split('/(?<=\\w)(?=[AZ])/', $str); } print_r(splitCamelCase("ZipPostalCode")); ?> 

Edit: ignore this - better answer the answer.

0
source
 $new = preg_replace('/([az])([AZ])/', '$1 $2', $old); 
0
source
 <?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'; ?> 
-3
source

Source: https://habr.com/ru/post/1335670/


All Articles