Categories: PHP

substr – PHP String Functions

Syntax :

substr ( string, start_position, length );

Description :

It’s an inbuilt function of PHP. substr() function will return part of a string.


Parameter :

  • string – This is a Required parameter. It is input string must be of minimum one character long.
  • start_position – This is a Required parameter. Tells from where to start the replacement in input string.
    • +ve number – Start replacing at the position specified. for example, in the string ‘xyzabc’, the character at 0 position is ‘x’, the char at position 3 is ‘a’, and so on.
    • -ve number – Start replacing at the specified position from the end of a string.
    • 0 (zero) – Start replacing at the first character in string.
  • length – This is an Optional parameter. It tells the length of the returned string. Default is same as the length of the string.
    • +ve number – The length to be returned from the start parameter.
    • -ve number – The length to be returned from the end of the string.
    • 0 (zero) – If the length is Zero(0), FALSE or NULL, an empty string will be returned.
    • Omitted – The substring from the start parameter till the end of string is returned.

Output :

Returns the extracted part of a string, or FALSE on failure, or an empty string.


ChangeLog :

[table caption=”” width=”100%” colwidth=”25%|75%” colalign=”left|left”]
PHP Version, Description
7.0.0, If string is equal to start characters long then an empty string will be returned. Prior to this version – FALSE was returned in this case.
5.2.2 , If the start parameter indicates the position of a negative truncation or beyond then false is returned.
5.2.6, Other versions get the string from start.
[/table]


Related articles : substr_replace(), wordwrap(), trim(), chop().


substr() – PHP Functions Example 1 :
<?php
echo substr("Hello from tutorialmines.",11);
?>

In above example, We get the string “tutorialmines.” as output from the input string “Hello from tutorialmines.”. Output of above code in the browser is as below:

tutorialmines.

substr() – PHP Functions Example 2 : Using different negative and position values for start length.
<?php 
echo substr("Hello from tutorialmines.",11)."<br>\n";
echo substr("Hello from tutorialmines.",5)."<br>\n";
echo substr("Hello from tutorialmines.",23)."<br>\n";
echo substr("Hello from tutorialmines.",8)."<br>\n\n"; 
 
echo substr("Hello from tutorialmines.",-1)."<br>\n";
echo substr("Hello from tutorialmines",-15)."<br>\n";
echo substr("Hello from tutorialmines",-4)."<br>\n";
echo substr("Hello from tutorialmines",-20)."<br>\n";
?>

Output of above code in the browser is as below:

tutorialmines.<br>
from tutorialmines.<br>
s.<br>
om tutorialmines.<br>

.<br>
m tutorialmines<br>
ines<br>
o from tutorialmines<br>


substr() – PHP Functions Example 3 :  Using all possible parameters
<?php

// PHP program to show 
// substr_compare() function 
echo substr("Hello from tutorialmines.",0,20)."<br>\n";
echo substr("Hello from tutorialmines.",5,15)."<br>\n";
echo substr("Hello from tutorialmines.",0,5)."<br>\n";
echo substr("Hello from tutorialmines.",10,10)."<br>\n\n"; 
 
echo substr("Hello from tutorialmines.",0,-1)."<br>\n";
echo substr("Hello from tutorialmines.",-15,-2)."<br>\n";
echo substr("Hello from tutorialmines.",0,-16)."<br>\n";
?>

Output of above code is :

Hello from tutorialm<br>
from tutorialm<br>
Hello<br>
tutorialm<br>

Hello from tutorialmines<br>
tutorialmine<br>
Hello fro<br>


substr() – PHP Functions Example 4 :  ERRORS and EXCEPTIONS – Returns FALSE on error.
<?php
var_dump(substr('ab', 3)); // bool(false)
?>

Output of above code is :

bool(false)


jyoti rani

Recent Posts

What Is a Progressive Web App? Why Would You Need One?

App usage is growing steadily without showing any signs of slowing down. Hence, it is no surprise that mobile applications…

12 months ago

7 Most Popular Paid Online Advertising Strategy

As the world has grown more digital, businesses have adapted themselves. An effectual adaptation includes online advertising. Offline advertising styles…

1 year ago

The Importance of User-Centered Design in Mobile App Development

Step into a world where apps dance to the user's tune. Picture Instagram, a photo-sharing sensation that swept the globe.…

1 year ago

Healthcare Mobile App Development: A Complete Guide for Founders

COVID-19 has led to a digitalization of lifestyle. As patients are taking their mental and physical health more seriously, healthcare…

1 year ago

Exploring Diverse WordPress Theme Niches: A Comprehensive Guide

Introduction WordPress, an immensely popular content management system (CMS), powers over 40% of the internet. What makes WordPress even more…

1 year ago

8 Awesome Blog Content Ideas for Movers to Skyrocket the SEO

For moving companies trying to capture their market share amidst stiff competition, a tip or two about what they can…

1 year ago