PHP substr Function

What is substr Function?

Explanation

In PHP, this function returns a part of a string.

Syntax:


substr(string,start,length)

In the above syntax given "string" is input to return , "start" specifies where to start in the input, "length" specifies the length of the part of a string.Usually the "start","length" can have positive and negative number,which refers to starting and ending of a string.

Example :


<?php
echo substr("abcdef", -1);
echo substr("abcdef", -2);
echo substr("abcdef", 0, -1);
echo substr("abcdef", 2, -1);
?>
Result :

f
ef
abcde
cde

In the above example first the start value is "-1" so last character "f" is taken, in another example start value is "0" so the string from the start is taken and since length is "-1" it leaves a character from the end to display output.

PHP Topics


Ask Questions

Ask Question