PHP Substr Example | PHP String substr() Function Tutorial

Like Tweet Pin it Share Share Email
PHP Substr Example | PHP String substr() Function Tutorial

PHP Substr Example | PHP String substr() Function Tutorial is today’s topic. The substr() function returns a part of a string. The substr() method extracts parts of the string, beginning at the character at the specified position, and returns the specified number of characters. The substr() method does not change an original string. If we want to extract characters from the end of the string, use a negative start number.

PHP Substr Example

PHP substr() function used to cut a part of a string from a string, starting at a specified position.

PHP substring is one of the most common PHP string functions. It returns the part of the string. The first argument specifies the string which will be trimmed. The second argument specifies the position of the trim start. The third argument specifies the length of a string to be returned. The start and length arguments can also be negative numbers.

The syntax of PHP String substr() method is following.

Syntax

substr(string, start, length)

The string parameter is required, and it specifies the string to return a part of.

The start parameter is required, and it is the position where to start the extraction. The first character is at index 0.

If start is non-negative, the returned string will start at the start‘th position in the string, counting from zero. For instance, in the string ‘abcdef,’ the character at position 0 is ‘a’ the character at position 2 is ‘c,’ and so forth.

If start is negative, the returned string will start at the start‘th character from the end of the string.

If the string is less than start characters long, FALSE will be returned.

The length parameter is optional and the number of characters to extract. If you omit the string length then, it extracts the rest of the string.

See the below example.

<?php

// app.php

echo substr("AppDividend", 3)."n";
echo substr("Medium", 2)."n";
echo substr("LaravelNews", 7)."n";
echo substr("Laracasts", 4)."n";

See the below output.

PHP Substr Example

Use Negative parameters in substr() function

Let’s pass start as the negative parameter.

<?php

// app.php

echo substr("AppDividend", -3)."n";
echo substr("Medium", -2)."n";
echo substr("LaravelNews", -7)."n";
echo substr("Laracasts",-4)."n";

See the below output.

Use Negative parameters in substr() function

If an integer is positive, it refers to start from start_position and extract the length from the beginning. If an integer is negative, then it refers to start from start_position and extract the length from the end of the string. If the length parameter is not passed, then the substr() function will return the string starting from the start_position till the end of the string.

For getting the substring of UTF-8 characters, I highly recommend mb_substr. See the following code.

<?php

// app.php

$utf8string = "appdæøå";

echo substr($utf8string,0,5)."n";
echo mb_substr($utf8string,0,5,'UTF-8');

See the output below.

PHP String substr() Function Tutorial

Finally, PHP Substr Example | PHP String substr() Function Tutorial is over.

Comments (0)

Leave a Reply

Your email address will not be published. Required fields are marked *