
- PHP 7 Tutorial
- PHP 7 - Home
- PHP 7 - Introduction
- PHP 7 - Performance
- PHP 7 - Environment Setup
- PHP 7 - Scalar Type Declarations
- PHP 7 - Return Type Declarations
- PHP 7 - Null Coalescing Operator
- PHP 7 - Spaceship Operator
- PHP 7 - Constant Arrays
- PHP 7 - Anonymous Classes
- PHP 7 - Closure::call()
- PHP 7 - Filtered unserialize()
- PHP 7 - IntlChar
- PHP 7 - CSPRNG
- PHP 7 - Expectations
- PHP 7 - use Statement
- PHP 7 - Error Handling
- PHP 7 - Integer Division
- PHP 7 - Session Options
- PHP 7 - Deprecated Features
- PHP 7 - Removed Extensions & SAPIs
- PHP 7 Useful Resources
- PHP 7 - Quick Guide
- PHP 7 - Useful Resources
- PHP 7 - Discussion
Explain substr() function in PHP
PHP offers different kinds of inbuilt functions having distinctive functionalities. The substr() is a built-in function in PHP and this function works with strings. It is utilized to extricate a part of the string .
The syntax of the substr() is demonstrated below.
substr(string,begin, length)
Now let's discuss the Parameters. Three parameters can be passed to substr() function out of which two are mandatory and one is optional.
string
In this parameter, we pass the string that necessities to cut or adjust. This is an obligatory parameter
begin position
This is a mandatory parameter. This implies the position of the string from where the part should be removed. This value should be an integer. In the event that integer is positive then the returned string will start from the starting position mentioned in the input.
In this case, a negative integer indicates that the start position is from the end of the string.
length
This parameter is discretionary and should be an integer. This means to the length of the part of the string that needs to be cut from the original string. In this case, a positive integer indicates that to begin from start and extract the length from the beginning.
, In this case, a negative integer indicates that the extracted length from the end of the string.
If the integer is negative then it means to begin from start and If nothing is passed, then it will return the string starting from start_position till the end of the string.
Example
<?php $string1 ="Welcome To Tutorials Point"; $len = strlen($string1); echo substr($string1, 8),"<br/>"; echo substr($string1, 5, $len),"<br/>"; echo substr($string1, -5, 3),"<br/>"; ?>
Output
To Tutorials Point me To Tutorials Point Poi
Explanation
In the above example, we have to take a string variable and then we have used substr() function to get the required portion of that string. In the first expression we have mentioned the starting point but no ending point. In the second expression, we have mentioned both the starting and ending point. In the third expression, we have mentioned a negative starting point so it evaluates from the end.
- Related Articles
- substr() function in PHP
- Explain array_merge() function in PHP.
- Explain array_intersect() function in PHP.
- Explain str_split() function in PHP
- Explain array_map() function in PHP
- How to use substr () in Android sqlite?
- Explain Polymorphism in PHP.
- Explain interface in PHP.
- Explain array_diff() in PHP
- Explain Encapsulation in PHP.
- strcmp() function in PHP
- strcoll() function in PHP
- strcspn() function in PHP
- strip_tags() function in PHP
- stripcslashes() function in PHP
