PHP – substr examples

PHP - substr examples

In this article lets explore substr examples in PHP.

We can use substr in many ways in PHP. Let’s check through examples what we can do using this function.

The basic syntax is as below :

substr(string $string, int $offset, ?int $length)

// $string - The input string from which you want to extract the substring.
// $start - The start position. First character is at index 0.
// $length - (optional) The length of the substring to be extracted.

1. Get substring from the middle of a string :

Check below code snippet to get substring from a string. We will find “codingsolutionspoint” from whole string.

$inputString = "";
echo $substring = substr($inputString, 8, 20); // Start at index 8 and extract 20 characters

// Output: "codingsolutionspoint"

2. Get last few characters from a string :

To get last few characters from given string use function as below :

$inputString = "Welcome to codingsolutionspoint";
$substring = substr($inputString, -20); // Extract the last 20 characters
echo $substring;

// Output: "codingsolutionspoint"

3. Get substring with negative length :

You can use negative length to give length from end of the string. Check below :

$inputString = "Welcome to codingsolutionspoint";
echo $substring = substr($inputString, 3, -6);

// Output: "come to codingsolution"

4. Get the substring up to a specific character.

You can use substr function to get string up to other character as below.

$inputString = "";
$atPosition = strpos($inputString, "/"); // Find the position of the "/" symbol
$substring = substr($inputString, 0, $atPosition); // Extract characters up to "/" position
echo $substring; 

// Output: "https:"

5. Get the first word from a sentence

We can use substr for example to get first word from a sentence using below syntax.

$inputString = "Welcome to codingsolutiospoint";
$firstWordEndPos = strpos($inputString, " "); // Find the position of the first space
$firstWord = substr($inputString, 0, $firstWordEndPos); // Extract the first word
echo $firstWord; 

// Output: "Welcome"

6. Get file extension from a filename

Many time we occur to find file extension and here is a way. Se below :

$filename = "csp.pdf";
$extensionStart = strrpos($filename, ".") + 1; // Find the position of the last dot and add 1
$fileExtension = substr($filename, $extensionStart); // Extract the file extension
echo $fileExtension;

// Output: "pdf"

7. Get substring between two specific characters :

When need to find string between two characters we can use as below :

$inputString = "The {csp} box";
$startPos = strpos($inputString, "{") + 1; // Find the position of the first "{" and add 1
$endPos = strpos($inputString, "}"); // Find the position of the first "}"
$substring = substr($inputString, $startPos, $endPos - $startPos); // Extract the text between "{" and "}"
echo $substring;

// Output: "csp"

8. Add read more ad shorten the length :

Many time mostly in blogging when need to add link as read more we can use below :

$inputString = "Hello, welcome to codingsolutionspoint, In this article lets explore various ways to use substr in PHP.";
$maxLength = 20;
if (strlen($inputString) > $maxLength) {
    $substring = substr($inputString, 0, $maxLength - 3) . "..."; // Limit to 20 characters and add "..."
} else {
    $substring = $inputString; // String is already within the length limit
echo $substring;

// Output: "Hello, welcome to..."

So that’s it. These are the various ways on how to use substr examples in PHP.

Check article how to find empty array.

See other articles on PHP here.