Home Tutorial Php Phpbasics Tutorial PHP SubString Function

 
 

PHP SubString Function
Posted on: February 24, 2010 at 12:00 AM
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

$superString="Thi is a text";

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

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

?>

 

Output:

Sub-string is :ex

Related Tags for PHP SubString Function:


Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.