CodingBison

This section discusses the last set of string functions that handle various miscellaneous tasks. For the sake of readability, we organize them further into three sets: (a) functions that handle substrings, (b) functions that manipulates case for strings, and (c) miscellaneous formatting functions.

Handling PHP Substrings

The first set of miscellaneous functions focus on substrings of a given string.

 /* Handling Substrings for Strings */
 $substring  = substr($larger_string, $start_index, $optional_length); 
 $int_count  = substr_count($larger_string, $substring);
 $new_string = substr_replace($larger_string, $new_string, $replace_index, $optional_length);

In the above list, substr() takes an index ($start_index) as an argument and returns a substring that begins at $start_index of the input string ($larger_string). substr() also takes an optional length to specify the length of the returned substring; by default, PHP returns entire substring starting from the passed index. The next function, substr_count() returns the number of times a substring appears in the given string. The last function, substr_replace() replaces a given substring with the new string. This function also takes an option index argument that specifies the string index at which the replacement is done.

Having described these functions, we provide a small program to show their usage. Here, we define a large string ($larger_string) and then use substr() function to retrieve specific substrings from it. Next, we use substr_count() to find out the number of times a substring appears in the larger string. Lastly, we use substr_replace() to substitute a substring in the larger string with another string.

 <?php
 $larger_string = "On Facebook, you can be friends even with your friend's friends.";
 echo "[] The original string is: $larger_string <br>";

 $start_index = 15; 
 $new_substring = substr($larger_string, $start_index);
 echo "[substr] The substring is: $new_substring <br>";

 $start_index = 3;
 $substring_length = 8;
 $new_substring = substr($larger_string, $start_index, $substring_length);
 echo "[substr] The substring is: $new_substring <br>";

 $sub_string = "friend";
 $substring_count = substr_count($larger_string, $sub_string);
 echo "[substr_count] The count for string, $sub_string, is: $substring_count <br>";

 $new_substring = "people whom you don't know.";
 $var_index = strpos($larger_string, "your friend");
 $new_substring = substr_replace($larger_string, $new_substring, $var_index);
 echo "[substr_replace] The substring is: $new_substring <br>";
 ?>

Here is the output:

 [] The original string is: On Facebook, you can be friends even with your friend's friends.
 [substr] The substring is: u can be friends even with your friend's friends.
 [substr] The substring is: Facebook,
 [substr_count] The count for string, friend, is: 3
 [substr_replace] The substring is: On Facebook, you can be friends even with people whom you don't know.

Case-Manipulation for PHP Strings

The second set of miscellaneous functions focus on manipulating case (lower/upper) of a string.

 /* Manipulating Case for Strings */
 $new_string = strtolower($var_string);
 $new_string = strtoupper($var_string);
 $new_string = ucwords($var_string);
 $new_string = ucfirst($var_string);

In the above list, strtolower() converts all characters of a given string into lower-case characters. On the other hand, strtoupper() converts all characters of a given string into upper-case characters. The remaining two functions are more specific. ucwords() converts the first character of all the words in a string to upper case. ucfirst() converts the first character of a given string to upper case.

We provide a small program that contains examples of these functions.

 <?php
 $var_string = "faCeBoOk";
 $new_string = strtolower($var_string);
 echo "[strtolower] The new string is: $new_string <br>";

 $new_string = strtoupper($var_string);
 echo "[strtoupper] The new string is: $new_string <br>";

 $new_string = ucfirst($var_string);
 echo "[ucfirst] The new string is: $new_string <br>";

 $new_string = ucfirst(strtolower($var_string));
 echo "[ucfirst(strtolower)] The new string is: $new_string <br>";

 $var_string = "facebook's mark zuckerberg";
 $new_string = ucwords($var_string);
 echo "[ucwords] The new string is: $new_string <br>";
 ?>

We provide the output below. Please note that sometimes, we need to ensure that the first character of the word is an upper case and the rest should be in lower cases (e.g. "Facebook"). This task can be achieved in two steps. As an example, the above program first uses strtolower() to convert "faCeBoOk" into all lower cases and then uses ucfirst() to change the first character to upper case.

 [strtolower] The new string is: facebook
 [strtoupper] The new string is: FACEBOOK
 [ucfirst] The new string is: FaCeBoOk
 [ucfirst(strtolower)] The new string is: Facebook
 [ucwords] The new string is: Facebook's Mark Zuckerberg

Formatting PHP Strings

This section discusses the last set of string functions that handle various miscellaneous tasks. For the sake of readability, we organize them further into three sets: (a) functions that handle substrings, (b) functions that manipulates case for strings, and (c) miscellaneous formatting functions.

Handling PHP Substrings

The first set of miscellaneous functions focus on substrings of a given string.

 /* Handling Substrings for Strings */
 $substring  = substr($larger_string, $start_index, $optional_length); 
 $int_count  = substr_count($larger_string, $substring);
 $new_string = substr_replace($larger_string, $new_string, $replace_index, $optional_length);

In the above list, substr() takes an index ($start_index) as an argument and returns a substring that begins at $start_index of the input string ($larger_string). substr() also takes an optional length to specify the length of the returned substring; by default, PHP returns entire substring starting from the passed index. The next function, substr_count() returns the number of times a substring appears in the given string. The last function, substr_replace() replaces a given substring with the new string. This function also takes an option index argument that specifies the string index at which the replacement is done.

Having described these functions, we provide a small program to show their usage. Here, we define a large string ($larger_string) and then use substr() function to retrieve specific substrings from it. Next, we use substr_count() to find out the number of times a substring appears in the larger string. Lastly, we use substr_replace() to substitute a substring in the larger string with another string.

 <?php
 $larger_string = "On Facebook, you can be friends even with your friend's friends.";
 echo "[] The original string is: $larger_string <br>";

 $start_index = 15; 
 $new_substring = substr($larger_string, $start_index);
 echo "[substr] The substring is: $new_substring <br>";

 $start_index = 3;
 $substring_length = 8;
 $new_substring = substr($larger_string, $start_index, $substring_length);
 echo "[substr] The substring is: $new_substring <br>";

 $sub_string = "friend";
 $substring_count = substr_count($larger_string, $sub_string);
 echo "[substr_count] The count for string, $sub_string, is: $substring_count <br>";

 $new_substring = "people whom you don't know.";
 $var_index = strpos($larger_string, "your friend");
 $new_substring = substr_replace($larger_string, $new_substring, $var_index);
 echo "[substr_replace] The substring is: $new_substring <br>";
 ?>

Here is the output:

 [] The original string is: On Facebook, you can be friends even with your friend's friends.
 [substr] The substring is: u can be friends even with your friend's friends.
 [substr] The substring is: Facebook,
 [substr_count] The count for string, friend, is: 3
 [substr_replace] The substring is: On Facebook, you can be friends even with people whom you don't know.

Case-Manipulation for PHP Strings

The second set of miscellaneous functions focus on manipulating case (lower/upper) of a string.

 /* Manipulating Case for Strings */
 $new_string = strtolower($var_string);
 $new_string = strtoupper($var_string);
 $new_string = ucwords($var_string);
 $new_string = ucfirst($var_string);

In the above list, strtolower() converts all characters of a given string into lower-case characters. On the other hand, strtoupper() converts all characters of a given string into upper-case characters. The remaining two functions are more specific. ucwords() converts the first character of all the words in a string to upper case. ucfirst() converts the first character of a given string to upper case.

We provide a small program that contains examples of these functions.

 <?php
 $var_string = "faCeBoOk";
 $new_string = strtolower($var_string);
 echo "[strtolower] The new string is: $new_string <br>";

 $new_string = strtoupper($var_string);
 echo "[strtoupper] The new string is: $new_string <br>";

 $new_string = ucfirst($var_string);
 echo "[ucfirst] The new string is: $new_string <br>";

 $new_string = ucfirst(strtolower($var_string));
 echo "[ucfirst(strtolower)] The new string is: $new_string <br>";

 $var_string = "facebook's mark zuckerberg";
 $new_string = ucwords($var_string);
 echo "[ucwords] The new string is: $new_string <br>";
 ?>

We provide the output below. Please note that sometimes, we need to ensure that the first character of the word is an upper case and the rest should be in lower cases (e.g. "Facebook"). This task can be achieved in two steps. As an example, the above program first uses strtolower() to convert "faCeBoOk" into all lower cases and then uses ucfirst() to change the first character to upper case.

 [strtolower] The new string is: facebook
 [strtoupper] The new string is: FACEBOOK
 [ucfirst] The new string is: FaCeBoOk
 [ucfirst(strtolower)] The new string is: Facebook
 [ucwords] The new string is: Facebook's Mark Zuckerberg

Formatting PHP Strings

The last set of miscellaneous functions handle string formatting.

 /* Miscellaneous Formatting String Functions */
 $new_string   = strrev($var_string); 
 $new_string   = str_repeat($var_string, $num_times); 
 $new_string   = str_pad($var_string, $total_length, $padding_characters, optional_flag); 

 $new_string   = trim($var_string, $delimiter); 
 $new_string   = ltrim($var_string, $delimiter); 
 $new_string   = rtrim$var_string, $delimiter(); 

 $token_string = strtok($var_string, $delimiter); 
 $new_array    = explode($delimiter, $var_string);
 $new_string   = impode($delimiter, $var_array); 

The first three functions in the above list do basic string formatting. strrev() reverses the input string. str_repeat() returns a string where the passed string is repeated for a specified number of times. str_pad() pads a given string with another strings. If the parameter $total_length is passed, then the padding is done until this length is reached. However, if $total_length is less than or equal to the length of the $var_string, then no padding is done. str_pad() also accepts an optional_flag that allows it to pad either at the beginning of the string or at the end of the string.

The next three functions in the above list do trimming of white-spaces from a string and in some ways are opposite of str_pad() function. trim() returns a new string that removes white-spaces from the start and the end. ltrim() returns a new string that removes white-spaces from the start. rtrim() returns a new string that removes white-spaces from the end. By default, these functions trim white-space character. However, we can specify a character different than the white-space character.

The last three functions in the above list split a string into smaller strings, combine strings from an array into one string, and split string variables from a given string variable. strtok() splits a given string into tokens that are separated by the string delimiter; we need to call strtok() multiple times to retrieve all the tokens. Along the same lines, explode() splits a given string based on a certain delimiter and places the substrings into an array. On the contrary, function implode() takes an array and joins all the elements of the array into one single string variable.

Here is an example that provides examples of strrev(), str_repeat(), and str_pad() functions.

 <?php

 $var_string = "Was it a hat I saw";
 $reverse_string =  strrev($var_string);
 echo "[strrev] The reversed string is: $reverse_string <br>";

 $var_string = "Friends";
 $str_repeated =  str_repeat($var_string, 4); 
 echo "[str_repeat] After repeating, the string is: $str_repeated <br>";

 $var_string = "Friends";
 $str_padded =  str_pad($var_string, 45, '..andmorefriends..');
 echo "[str_pad] After padding, the string is: $str_padded <br>";

 $var_string = "Friends";
 $str_padded =  str_pad($var_string, 20, '...', STR_PAD_LEFT);
 echo "[str_pad (STR_PAD_LEFT)] After padding, the string is: $str_padded <br>";
 $str_padded =  str_pad($var_string, 20, '...', STR_PAD_RIGHT);
 echo "[str_pad (STR_PAD_RIGHT)] After padding, the string is: $str_padded <br>";
 ?>

We provide below the output of the above snippet. Please note that for str_pad(), the length passed (second argument) is the total length of the returned string and it includes the length of the original string plus the padding. Thus, when we pad "Friends" with "..." and pass a length of 20, then the returned string "Friends............." and has a total length of 20. We can also provide STR_PAD_LEFT or STR_PAD_RIGHT to specify if we want the padding to be done at the beginning or at the end of the string respectively. By default, the padding is done at the end of the string.

 [strrev] The reversed string is: was I tah a ti saW
 [str_repeat] After repeating, the string is: FriendsFriendsFriendsFriends
 [str_pad] After padding, the string is: Friends..andmorefriends....andmorefriends....
 [str_pad (STR_PAD_LEFT)] After padding, the string is: .............Friends
 [str_pad (STR_PAD_RIGHT)] After padding, the string is: Friends............. 

The last implementation of this section handles the remaining formatting functions: trim(), ltrim(), rtrim(), explode(), and implode(). For the trim-related functions, the example attempts to trim the extra dot (".") from the string.

 <?php
 $str_username = ".... The username is jwayne .......";
 echo "The string is: $str_username <br>";

 $str_trimmed = trim($str_username, ".");
 echo "[trim] The trimmed string is: $str_trimmed <br>";

 $str_trimmed = ltrim($str_username, ".");
 echo "[ltrim] The left trimmed string is: $str_trimmed <br>";

 $str_trimmed = rtrim($str_username, ".");
 echo "[rtrim] The right trimmed string is: $str_trimmed <br>";

 $str_userdata="name:John Wayne;age:28;city:Fargo;zip:58104;username:jwayne";
 $str_token = strtok($str_userdata, ";");
 while ($str_token) {
     echo "[strtok] $str_token<br>";
     $str_token = strtok(";");
 }

 $str_usernames = "jwayne jstewart jbridges";
 $ret_usernames = explode(' ',$str_usernames);
 var_dump($ret_usernames);
 echo "<br>";

 $arr_usernames = array("jwayne", "jstewart", "jbridges");
 $ret_string = implode(':',$arr_usernames);
 var_dump($ret_string);
 ?>

Here is the output:

 The string is: .... The username is jwayne .......
 [trim] The trimmed string is: The username is jwayne
 [ltrim] The left trimmed string is: The username is jwayne .......
 [rtrim] The right trimmed string is: .... The username is jwayne
 [strtok] name:John Wayne
 [strtok] age:28
 [strtok] city:Fargo
 [strtok] zip:58104
 [strtok] username:jwayne
 array(3) { [0]=> string(6) "jwayne" [1]=> string(8) "jstewart" [2]=> string(8) "jbridges" }
 string(24) "jwayne:jstewart:jbridges" 




comments powered by Disqus