SQL PHP HTML ASP JavaScript articles and free scripts to download
 
 

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.

Discuss this tutorial at forum


PHP String Functions
All String Functions in PHP
str_replace: How to replace a part of a string with another string
str_ireplace: Case in-sensitive search and replace using array of strings
strlen: How to find length of a string in PHP?
trim: Removing empty space from both sides of a string
strrev: Reversing a string by using strrev function in PHP
Adding two or more strings in PHP
stristr: Searching for a presence of a string inside another string
nl2br: Adding Line breaks inside a string in place of carriage returns
split: Breaking a string to form array using delimiters
substr: Collecting part of a string
substr_count: Counting occurrence of sub string in main string
str_repeat: Repeating a string number of times
strtolower(): function to change alphabets to Lower case
strtoupper(): function to change characters to upper case letters
function to collect part of a string with two landmarks
Random string generator with number and alphabets for password
strcasecmp(): Case insensitive string comparison
strcmp(): Case sensitive string comparison
str_pad(): String pad with specified length
md5 hash of a string: encryption of a string
Separating domain and userid part from an email address using split
htmlspecialchars: Printing html special chars to the page
strip_tags: Removing html tags within a string
ucwords: Removing html tags within a string



 

Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.

Scripts
PHP
JavaScript
PHP Tutorial Index
String Functions
htmlspecialchars
md5 hash
nl2br
Random string
strip_tags
str_replace
str_ireplace
strlen
strrev
stristr
split
substr
substr_count
str_repeat
strtolower
strcasecmp
strcmp
str_pad
trim
ucwords
Popular Tutorials
Drop down list
File Upload
Signup script
Member Login
Line Graph
PHP MySQL Paging
PHP Calendar
PHP Tutorials
Date & Time
Array
String Functions
Math Functions
Form Handling
File Handling
Comment Posting
Content Management
Subscribe
Submit your email address and receive article and product notifications. Your email is safe with us.