PHP SubString Function


 

PHP SubString Function

In the current tutorial we will come to know about how to extract In PHP to find out a sub-string from of a string, we use substr() function. The format of this function is as follows string substr ( string $var , int $init [, int $len ] )

In the current tutorial we will come to know about how to extract In PHP to find out a sub-string from of a string, we use substr() function. The format of this function is as follows string substr ( string $var , int $init [, int $len ] )

PHP Sub String Function:

In PHP to find out a sub-string from of a string, we use substr() function. The format of this function is as follows:

string substr ( string $var , int $init [, int $len ] )

In the above format $var denotes the actual string from which the sub-string is to be extracted. $init denotes the initial index value of the string, if we give any non-negative value then it will considered as the "th" character starting from 0, like if the string is "roseindia" and the value of $init is 3 the the first character will chopped is "i", and $len denotes the final index value of the string upto which the string has to be chopped. The value of $len could be negative. With the help of the example the concept could be more precise.

Example:

<?php

$superString="Thi is a text";

$substring=substr($superString,5);

echo "Sub-string is :".$substring."<br/>";

?>

Output:

Sub-string is :s a text

Example:

<?php

$superString="Thi is a text";

$substring=substr($superString,-1);

echo "Sub-string is :".$substring."<br/>";

?>

Output:

Sub-string is :t

Example:

<?php

$superString="Thi is a text";

$substring=substr($superString,3,-1);

echo "Sub-string is :".$substring."<br/>";

?>

Output:

Sub-string is : is a tex

Example:

<?php

0

$superString="Thi is a text";

$substring=substr($superString,-3,-1);

echo "Sub-string is :".$substring."<br/>";

1

?>

 

Output:

2

Sub-string is :ex

Ads