SQL PHP HTML ASP JavaScript articles and free scripts to download
PHP String Functions

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.

For example from a string of characters we want to copy a part of the string then we can use the stastr function. Let us take a string having values of “abcdefghijklmno”.
$string="abcdefghijklmno";
$string=substr($string,0,4);
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.
$string="abcdefghijklmno";
$string=substr($string,2,4);
echo $string;
This will print cdef. Here as we have used 2 , so 4 characters starting from 2 character will be taken.

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

$string="abcdefghijklmno";
$string=substr($string,-6,4);
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.

Be the first to post comment on this article :


Google+
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked



Join Our Email List
Email:  
For Email Newsletters you can trust
HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer