Tag Archives: string
Convert variable to string in PHP

Convert variable to string in PHP PHP is a loosely typed language and assigns types to variables depending what is assigned to it. Sometimes you will want to transform a given variable into a string format and obtain a similar output with “ToString()” method in Java or .NET. This could be done easily with one of the following
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) ); |
How to strip all spaces out of a string in php

How to strip all spaces out of a string in php You have a string stored in a PHP variable, and you want to eliminate all whitespaces or spaces from that string with PHP, not only from the beginning or the end of the string. Scenario
1 2 3 4 5 6 |
<?php //You want to strip all spaces from the below string and obtain "thisisjustastring" $firstString= 'this is just a string'; ?> |
Solution for spaces
1 2 3 4 5 6 7 |
<?php $firstString= 'this is just a string'; $newString = str_replace(' ', '', $firstString); echo $newString; //outputs "thisisjustastring" as a string ?> |
Solution for whitespaces
1 2 3 4 5 6 7 |
<?php $firstString= 'this is just a string'; $newString = preg_replace('/\s+/', '', $firstString); echo $newString //outputs "thisisjustastring" as a string ?> |
Converting strings to numbers in javascript

Converting strings to numbers in javascript There are some situations where you would want to take a string from a place, convert it to a number and put it in another place on the client side. This thing can be obtained by some helper function that are built-in in javascript. In this article we will