PHP Change String value behaviour or look

PHP How to Change Strings.Changing and Manipulate Strings in  PHP by using PHP Built-in Functions, strtoupper() ,strtolower()  ,strrev() ,ucfirst(), ucwords() and str_replace()

You want to force a string to uppercase, meaning you want to capitalize a string.

You can achieve that using the strtoupper()

strtoupper function changes every letter of a string to capital letter.
Usage: the function takes one string as a parameter input and returns a capitalized string.

<?php 
$string = 'arsenal is in good shape now'; 
echo strtoupper($string); 
?>

Output : ARSENAL IS IN GOOD SHAPE NOW

You want to force a string to lowercase, meaning you want to change a string to small letters.
You can achieve that using the strtolower() function.
strtolower() function changes every letter of a string to small letter.

Usage: the function takes one string as a parameter input and returns a small capitalized string.

//Lowercase entire string.

<?php 
$string = 'ARSENAL IS IN GOOD SHAPE NOW';
echo strtolower($string); 
?>

Output: arsenal is in good shape now
You want to change the first charater of a string to big capital letter.
The ucfirst function takes a string an a parameter input and returns the string with its first character capitalized. Capitalize (make it big) the first character / letter of every word. Use the  ucwords() function. The ucfirst() function takes a string an a parameter input and returns the modified string

<?php 
$soccer = "arsenal is in good shape now";
//Uppercase each word in the string 
$str = ucwords($soccer); echo str;
?>

Output: Arsenal is in Good Shape Now

want to replace all occurrences of searched string with the a replacement string.

<Use the str_replace() function. The str_replace function has three parameters that are necessary for the function
to work smoothly.

str_replace($search, $replace, $subject)

Parameters:-

  • search -> this is the value of string you searching for, it can either be a string or an array
  • replace -> the replacement value that replaces found search values. An array may be used to designate multiple replacement
  • subject -> this is the original string where search and replacement operations happen. After calling the function, the modified original string will be returned.
<?php
$colours = "black,yellow,blue,black,white,black";
$search = "black"; //search string
$replace = "red"; //replacement string
/*
* search through the string of colours and
*change all occurrences off
* the word 'black' with 'red'
*/
$newStr = str_replace($search, $replace, $colours);
echo $newStr;
?>

Output: red,yellow,blue,red,white,red

Trimm white space (Blanks) from a String
When You want to remove whitespace from the beginning or end of a string. For instance, you want to remove all trailing space and clean up user input before you validate it.
Use the functions stltrim(), rtrim(), or trim() .

  • ltrim() function removes whitespace from the beginning of a string
  • rtrim() from the end of a string
  • trim()   from both the beginning and end of a stringThe three above written functions remove whites pace, tabulator, newline and etc. The following table shows
    the Default characters removed by  trim(), ltrim(), and rtrim()

The three above written functions remove whites pace, tabulator, newline and etc. The following table shows
the Default characters removed by trim(), ltrim(), and rtrim()

CharacterASCII valueMeaning
" "0x20Space
"\t"0x09Tab
"\n"0x0ANewline
"\r"0x0DCarriage return
"\0"0x00NUL-byte
"\x0B"0x0BVertical tab

 

<?php
 $learn = " Programming the PHP \n in Modern way";
$leftTrimmed = ltrim($learn); // output is "Programming the PHP"
?>

The white space of the left side of the string is removed  but \n line is considered and output is as follows.

Programming the PHP 
in Modern way

 

 

 

Strip and remove White-space from text using preg_replace().

The following function stripWhitespace($text) uses the PHP built-in feature of the regular expression. What it does is search for the text within the two forward slash characters ,/, and then replaces any it finds with a single space. Between the slashes is the simple string \s+, which means find any section of white-space that is one or more characters in length. The \s stands for a white space character and the + indicates that the preceding character should appear one or more times in the search.
The actual string passed to the preg_replace() function is modified and then returned to the calling code.

<?php
function stripWhitespace($text)
{
return preg_replace('/\s+/', ' ', $text);
}
//using the function
echo stripWhitespace($text);
?>

 

 

Leave a Reply