How to Extract character from a String using php substring ?

PHP offers a variety of functions, including the PHP substring function, to make it easy to work with different data types. PHP substring, also known as PHP Substr(), is the most common PHP string function and it is used to extract a part of a string. The PHP substring function accepts a string and returns a substring from the string. Here we will discuss how to extract a substring from a string in PHP using Substr().

Syntax

The PHP substring has the following syntax.

substr(string, start, length)

Parameters

The syntax of the substr() function contains three parameters, $string, $start and $length, in which the first two are mandatory, and the last one is optional.

$string

$string is the main string parameter. It specifies the string that needs to be changed or removed. $string is a mandatory parameter in the PHP substring function and it takes the original string value to extract substring from a string in PHP.

$start

$string is the second mandatory parameter in PHP substring function and it specifies the starting position from where the substring will be extracted. The input is an integer and it has following conditions.

 

  • If $start has a positive value, the returned string starts from the $start position in the string, and counting begins from zero.
  • If $start has a negative value, the returned string starts from $start character position from the end of the string, and counting begins from -1.
  • If the $string size is less than $start value, it returns FALSE.

$length

$length is an optional parameter of the PHP substring function and it specifies the length of the string to be cut. The input is an integer and it has the following conditions.

 

  • If $length is positive, the returned string contains the number of characters passed in $length parameter beginning from the $start.
  • If $length is negative, it begins from $start position and extracts the length from the string’s end. Many characters omitts from the end of the string if the length passed is negative.
  • If $length parameter is zero, FALSE or NULL will be returned.
  • If the $length parameter is not passed, substr() function returns a string from $start till the end of the string.

How to Extract a Character From a String using PHP substring function

 

Now you know all the parameters used in the PHP substring function, let’s see some examples of how you can extract characters from a string using PHP substring function.

1. Use of Substring Function with Positive Starting Position

This example shows how the PHP substring function is used with the positive starting position. Below, as the starting position is positive, the counting will start from 0. 

<?php

     $string = "Welcome to Techaxis";

     $sub string = substr( string, 1 );

     echo " lt;h3> The original string value is:<b > $string </b></h3>";

     echo " lt;h3> The substring value is:<b > $sub_string </b></h3>";

?>

So when the starting position is 11, the string will be cut from the 12th position to the original string’s end. When you run the script from the web server, the following output will come.

2. Use of Substring Function with Negative Starting Position

This example shows how the PHP substring function is used with the negative starting position to extract a substring from a string. As the starting position is negative, the counting startsed from the end of the string, and the value 1.

<?php

     $string = "I like programming";

     $sub string = substr( string, -1 );

     echo " lt;h3> The original string value is:<b > $string </b></h3>";

     echo " lt;h3> The substring value is:<b > $sub_string </b></h3>";

?>

The script, in this case, will return a substring from the starting position to the string’s end. When you run the script from the web server, the following output will come.

 

3. Use of Substring Function with Positive Starting Position and Length

This example shows how the PHP substring function is used with a positive starting position and length. In the following example, the starting position is 21, and the length is 9. Here, the starting position is extracted by counting from 0 to 21, and the ending position is extracted by counting 9 characters.

 

<?php

     $string = "PHP is a server-side scripting language";

     $sub string = substr( string, 2 , 9);

     echo " lt;h3> The original string value is:<b > $string </b></h3>";

     echo " lt;h3> The substring value is:<b > $sub_string </b></h3>";

?>

When you run this script from the web server, the following output will come.

 

4. Use of Substring Function with Negative Starting Position and Length

This example shows how the PHP substring function is used with the negative starting position and length. In the following example, the starting position is -16, and the length is -9. Both the starting position and the length will count from the original string’s end and the counting will start from 1.

<?php

     $string = "HTML is a mark-up language";

     $sub string = substr( string, -1 , -9);

     echo " lt;h3> The original string value is:<b > $string   </b></h3>";

     echo " lt;h3> The substring value is:<b > $sub_string </b></h3>";

?>

When you run this script from the web server, the following output will come.

 

5. Use of Substring Function with Negative Starting Position and Positive Length

This example shows how the PHP substring function is used with the positive starting position and negative length. As you can see, here the starting position is -11, and the length is 7. So, the starting position is extracted by counting from the string’s end. The substring will be cut by counting 7 characters after the starting position.

<?php

     $string = "Javascript is very popular now";

     $sub string = substr( string, -1 , 7);

     echo " lt;h3> The original string value is:<b > $string </b></h3>";

     echo " lt;h3> The substring value is:<b > $sub_string </b></h3>";

?>

When you run this script from the web server, the following output will come.

 

 

Now you may know how to extract characters from a String using the PHP substring function. We hope you can now apply the substr() function efficiently. If you are still confused about the substr() function in PHP, you can join PHP course in Nepal at TechAxis. TechAxis is considered to be the best institute for PHP training in Nepal. We provide you with a new learning scope on PHP. We have years of experience in offering PHP training in Kathmandu.