Search⌘ K
AI Features

Getting Portions of a String

Explore how to extract and manipulate substrings using Laravel's substr helper method. Understand the use of positive and negative start indices, optional length parameters, and applications like shortening Git commit hashes in user interfaces.

The substr helper method

We can use the substr helper method to retrieve portions of an existing string. This helper defines a $start argument and an optional $length argument.

PHP
<?php
/**
* Versions: Laravel 8, 9
*
* @return string
*/
public static function substr(
string $string,
int $start,
int|null $length = null
);

If the start value is positive, the method will return text starting at that location in the ...