W3Schools.com

PHP substr() Function


PHP String Reference Complete PHP String Reference

Definition and Usage

The substr() function returns a part of a string.

Syntax

substr(string,start,length)

Parameter Description
string Required. Specifies the string to return a part of
start Required. Specifies where to start in the string
  • A positive number - Start at a specified position in the string
  • A negative number - Start at a specified position from the end of the string
  • 0 - Start at the first character in string
length Optional. Specifies the length of the returned string. Default is to the end of the string.
  • A positive number - The length to be returned from the start parameter
  • Negative number - The length to be returned from the end of the string


Tips and Notes

Note: If start is a negative number and length is less than or equal to start, length becomes 0.


Example 1

<?php
echo substr("Hello world!",6);
?>

The output of the code above will be:

world!


Example 2

<?php
echo substr("Hello world!",6,5);
?>

The output of the code above will be:

world


PHP String Reference Complete PHP String Reference
WEB HOSTING
Best Web Hosting
PHP MySQL Hosting
Best Hosting Coupons
UK Reseller Hosting
Cloud Hosting
Top Web Hosting
$3.98 Unlimited Hosting
Premium Website Design
WEB BUILDING
XML Editor - Free Trial!
FREE Website BUILDER
Best Website Templates Top CSS Templates
CREATE HTML Websites
EASY WEBSITE BUILDER
W3SCHOOLS EXAMS
Get Certified in:
HTML, CSS, JavaScript, XML, PHP, and ASP
W3SCHOOLS BOOKS
New Books:
HTML, CSS
JavaScript, and Ajax
STATISTICS
Browser Statistics
Browser OS
Browser Display
SHARE THIS PAGE