PHP substr Function



PHP Tutorial > String Functions > substr Function

The substr function in PHP is used to retrieve a portion of the string. The syntax of the substr function is:

substr ('string', position_start, substr_length)

where position_start is the starting point (the first character has a position of 0). if position_start is a negative number, the starting position is the -(position_start)th character from the end. substr_length is the length of the string to be returned. If substr_length is a negative number, that means substr_legnth characters will be omitted from the end of the string.

Let's take a look at the examples below:

Example 1

print substr('PHP substr is a substring function', 5, 3);

Result:

ubs

Example 2

print substr('PHP substr is a substring function', -5, 3);

Result:

cti

Example 3

print substr('PHP substr is a substring function', 11, -5);

Result:

is a substring fun

11 means the returned string starts at the 11th position, which is "i" in is. -5 means the last 5 characters in the string are omitted.

Next: PHP trim Function

Link to this page: If you find this page useful, we encourage you to link to this page. Simply copy and paste the code below to your website, blog, or profile.




More 1Keydata Tutorials



PHP addslashes
PHP echo
PHP explode
PHP htmlentities
PHP htmlspecialchars
PHP implode
PHP md5
PHP number_format
PHP print
PHP str_replace
PHP strlen
PHP strpos
PHP strstr
PHP substr
PHP trim

PHP Syntax

PHP Sitemap

PHP Resources