The PHP substring function, substr, allows a programmer to retrieve a specified portion of a string known as a substring. String manipulation is a very important and sometimes tricky task so care must be taken to ensure that the functions are properly used and understood.
We usually need to use the PHP substring function anytime we want to break a larger string into a smaller string.
The tutorial below will show you how to properly use the PHP substring function and will help get you up to speed with PHP substrings.
The syntax of the PHP substring function, substr, is shown below:
$substring = substr($original, $start, $length)
Where $substring is the result after performing the function, $original is the original string, and:
Parameter | Description |
---|---|
$start |
Required. Tells where to start in the original string.
|
$length | Optional. Tells the length of the substring to be returned. If no $length is specified, everything from the starting point to the end of the string will be returned.
|
You do not need to memorize the boring syntax above immediately. Instead, look at our PHP substring examples below to see how it actually works.
Take a look at the following example. The comments in the code are a guide to what each substring instruction does:
<?php // declare the string we will be breaking into substrings $string = 'I love Tutorial Arena so much'; $result1 = substr($string, 2); // from 2nd character to the end $result2 = substr($string, 7); // from 7th character to the end // start at the 7th character and then be 8 characters long $result3 = substr($string, 7, 8); echo $result1 . '<br />'; echo $result2 . '<br />'; echo $result3 . '<br />'; ?>
The result:
The examples above show how to use the PHP substring function with positive parameters only. Negative parameters are less frequently used but we look at them nonetheless below.
The example below shows how to use the PHP substring function with negative parameters. Once again, the comments in the code are a guide to what each substring instruction does.
<?php // declare the string we will be breaking into substrings $string = 'abcdefg'; $result1 = substr($string, -3); // start 3 characters from the end $result2 = substr($string, -5); // start 5 characters from the end // start 5 characters from the end, then omit the last 2 characters $result3 = substr($string, -5, -2); echo $result1 . '<br />'; echo $result2 . '<br />'; echo $result3 . '<br />'; ?>
The result will be:
As you just saw in the example above, using the PHP substr function with negative parameters is not as hard as it would first seem. Just keep the syntax in mind a practice with a bit of code to confirm your understanding.
If we want to retrieve only a single character from a string we do not even need to use substr in PHP. Take a look:
<?php $string = 'I love Tutorial Arena so much'; echo substr($string, 7, 1); // start at 7th char and 1 char long echo '<br />'; echo $string[7]; // return 7th character in string ?>
You will get:
If you are extracting only a single character, it saves you code if you treat the string as an array of characters and retrieve the character you want using its location in the string. Remember that a string in PHP is treated as an array of characters and that is why this is possible.
As we just learnt above, we are able to extract a character from a string without the use of substr in PHP. This concept can be adapted quite easily to return a substring from a string without the use of the PHP substr function at all.
To do this, we would have to get what we want character by character. Have a look below:
<?php $string = 'I love Tutorial Arena so much'; $start = 7; // where in the string to start $length = 8; // how long our substring should be $answer = ''; // variable used to hold our answer // loop through string from $start to $length and get characters for ($i = $start; $i < ($start + $length); $i++) $answer .= $string[$i]; echo $answer; ?>
The result:
As you can see, we actually iterated through our original string starting at the character we want, then pulling each character from the current location and appending it to the variable which holds our answer.
Remember that when working on strings (as with arrays), the first character is always counted as 0. This little rule is easy to forget as it may seem counter-intuitive to some.
We hope this PHP substr tutorial was helpful.
Citation: http://php.net/manual/en/function.substr.php