Substr() Function in PHP

Last Updated On Thursday 9th Sep 2021

PHP substr

  • This function is used to return a part of string.
  • Its Very Simple Syntax substr()

PHP String Contains substring

	substr(string,start,length);
	

It has few Parameters

  • First one is String – Specify the Input String.
  • Second one is a start – It is the required Parameter.
  • Third one is length – Optional Parameter. Specify the end of the string
	<?php
echo substr("Hello Folks", 5); // Folks

?>
	

Starts With 0 – Remove all five characters from the start and return the remaining. ( includes whitespace )

	<?php
echo substr("Hello Folks", 5,4); // Fol

?>
	

In Above Example returns only three characters, but the length is 4 because it includes the whitespace.

	<?php
echo substr("Hello Folks", 5,-1); // Folk

?>
	
  • In Above Code, The Negative Number is going to return the last index
  • I Specify -1 it’s going to return from the end of the string
	<?php
echo substr("Hello Folks", 5,0); // 

?>
	
  • In Above Example, that is going not to return anything
  • We Choose the length as 0

Use Case

We can use this anywhere in the PHP, Where you Want to subtract some text from the element