Collecting sub string from any part of a string

We can take out a part of a string by using substr function in PHP. This way starting from any location to any location we can copy the string. Here is the syntax of substr function.

string substr (string string, int start [, int length])

We can see the optional element of length which will collect that many length of string.

Removing part of the string from starting

echo $string;
Output of above code is efghijklmno

Copying part of the string from starting

For example from a string of characters we want to copy a part of the string then we can use the substr function. Let us take a string having values of “abcdefghijklmno”.

echo $string;
This will print abcd to the string. Here we have set the staring element as 0 so from the beginning 4 characters are collected.

Removing Part of the string after x chars

echo $string;
This will print cdef. Here as we have used 2 , so 4 characters starting from 2 character will be taken.

Part of the string from the end

By using negative number in place of starting location we can pick up sub strings from the end of the string. Like this

echo $string;
The above line of code will return jklm. You can see the negative number 6 mean the function will start collecting from the 6th position from the end. That is the from the character j . Now from j 4 characters mean it is jklm.

How last chars removed from a string by using substr()

Your Rating


PHP String Functions

Post Comment This is for short comments only. Use the forum for more discussions.

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2017 All rights reserved worldwide Privacy Policy Disclaimer