Friday, April 6, 2012

Use of substr() PHP function

Use of substr() PHP function 


PHP is having a very cool function to substract the length of a string. Use of substr() PHP function is very easy and simple. See the below example to know how it works.

PHP Code:     
<?php
$QUERY 
"Substract starting five letters from me.";
   $QUERY substr($QUERY'0''5');
echo 
$QUERY;?>
The above code will substract the first five letters from the string.
The output of the above code will be:
Code:
Subst 
Similarly we can substract a part of any string using this function. Just specify a start point and an end point. look at the below example for better explaination.
PHP Code:

<?php
$QUERY 
"Substract any five letters from me."   $QUERY substr($QUERY'10''15');
echo 
$QUERY;?>
The above code will substract all letters coming after 10 and upto 15th character from the string.
The output of the above code will be:
Code:
any five letter 
Similarly we can use it in this way,
Code:
<?php
$rest 
substr("abcdef", -1);    // returns "f"
$rest substr("abcdef", -2);    // returns "ef"
$rest substr("abcdef", -31); // returns "d"
?>
 Try it yourself and you will enjoy learning PHP ..

No comments:

Post a Comment