Monthly Archives: December 2013
How to strip spaces out of a string in perl
How to strip spaces out of a string in perl You have a variable string that may have blank spaces at the end of the string or at the start of the string and you need to remove these spaces so that you can make use properly of the value of that variable. Scenario
1 2 |
//You want to strip spaces from the left side and right side of the bellow string and obtain "username" $val_string= ' username '; |
Programming jokes
Programming jokes A few months ago I wrote an article about funny comments encountered in different applications taken from the web. The holidays are coming so it is time to have more fun and relax a bit. Let’s spend some time on an article about programming jokes. These are all found over the internet, and
How to explode a string on upper case characters
How to explode a string on upper case characters Problem You have a string with the following value “thisStringMustBeSplittedAfterUpperCase” and you want to obtain every word separately in an array, splitted after upper case. Solution
1 |
$chunks = preg_split('/(?=[A-Z])/', $string, -1, PREG_SPLIT_NO_EMPTY); |
Output The output of the $chunks array obtained with a var_dump($chunks) will be:
1 2 3 4 5 6 7 8 9 |
array (size=8) 0 => string 'this' (length=4) 1 => string 'String' (length=6) 2 => string 'Must' (length=4) 3 => string 'Be' (length=2) 4 => string 'Splitted' (length=8) 5 => string 'After' (length=5) 6 => string 'Upper' (length=5) 7 => string 'Case' (length=4) |
Solution in a function
1 2 3 4 5 6 |
function upperCaseSplit($string){ return preg_split('/(?=[A-Z])/', $string, -1, PREG_SPLIT_NO_EMPTY); } $string = 'thisStringMustBeSplittedAfterUpperCase'; var_dump (upperCaseSplit($string) ); |