substr() : sub string from any part of a string

Extract part of a string

substr() returns part of a string using a zero-based offset and an optional length. Negative offsets count from the end.

$text = 'plus2net.com';
echo substr($text, 0, 8); // plus2net
echo substr($text, -3);   // com
$string="abcdefghijklmno";
$string=substr($string,4);
echo $string; // Output is efghijklmno 
Syntax
substr (string $string, int $start [, int $length])
Use substr() to extract a byte-based portion of a string, starting at a specified position. The optional length limits the returned portion. For multibyte character offsets, use mb_substr() when the mbstring extension is available.
ParameterDESCRIPTION
$stringRequired : Input string
$startRequired : if not negative then return sting will start from $start position.
if negative then return string will start from $start characters from end of the string.
if the start offset exceeds the string length, PHP 8 returns an empty string (""); older PHP versions may differ.
The first char is 0 position from left.
$lengthOptional : if Positive then starting from $start, $length number of chars are returned.
if negative then $length number of chars will be omitted from end of the string.
if $length is 0, the result is empty. Avoid passing false or null as a length; use an explicit integer or omit the argument.
if $length is omitted then the string starting from $start till the end is returned.

Copying part of the string from starting till a position

$string="abcdefghijklmno";
$string=substr($string,0,4);
echo $string; // Output abcd 
This will print abcd to the string. Here we have set the staring element as 0 so from the beginning 4 characters are collected.

Part of the string after x chars ( positive $start , positive $length)

$string="abcdefghijklmno";
$string=substr($string,2,4);
echo $string; // Output cdef 
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, negative $start

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

Negative $start, positive $length

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

Negative $start, negative $length

$string="abcdefghijklmno";
$string=substr($string,-6,-1);
echo $string; // Output jklmn
The above line of codes will return jklmn. 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 all chars except last char, mean it is jklmn.
Using substr() we can collect part of the string of any length from any position. However to collect part of the string we need to know the position of the searched string. To know the position of any searched string from starting or ending we have to use stripos()

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

String Functions Extract text between markers


Subscribe to our YouTube Channel here



plus2net.com











PHP video Tutorials
✖
We use cookies to improve your browsing experience. . Learn more
HTML MySQL PHP JavaScript ASP Photoshop Articles Contact us
© 2000-2026 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer