How do you pull first 100 characters of a string in PHP

How do you pull first 100 characters of a string in PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Trimming Strings in PHP: The substr() Reality Check

When you need to truncate a string variable, such as extracting the first 100 characters for display or logging, the instinctive approach often leads down a rabbit hole of string manipulation functions that feel like they were designed by someone who overthought a simple task. You might wonder if there is a dedicated 100charfunction() syntax similar to JavaScript's .slice() or Python's string slicing. In PHP, however, the solution lies in a function that has been reliable for two decades: substr().

The canonical way to achieve this is by calling substr($string, 0, 100). The parameters are strict about their semantics and order. The first argument is always the source string. The second argument specifies the starting offset. For zero-based indexing, which PHP uses, passing 0 tells the function to begin at the very start of the string. The third argument defines the length. Therefore, substr($string1, 0, 100) returns a new string containing exactly 100 characters from the beginning. If the original string is shorter than 100 characters, PHP gracefully handles this by returning the entire string without throwing an error or padding with spaces.

$string1 = "I am looking for a way to pull the first 100 characters from a string variable to put in another variable for printing.";
$string2 = substr($string1, 0, 100);

echo $string2; 
// Output: I am looking for a way to pull the first 100 charact

While substr() is robust, it operates on bytes rather than characters. This distinction becomes critical when dealing with UTF-8 encoded strings containing multi-byte characters, such as emojis or accented letters. If you blindly take the first 100 characters of a string like "👨‍💻", substr() might cut through the middle of the sequence because it counts bytes, not logical characters. This can result in malformed output where an emoji appears split across lines or displays as a question mark box.

To handle this safely in modern PHP versions (7.4 and later), you should utilize the mb_substr() function. The Multibyte String extension provides character-aware slicing that respects the encoding of your string. You must ensure the encoding is set correctly, typically via mb_internal_encoding('UTF-8') or by passing the encoding as the fourth argument to substr in older contexts, though mb_substr is generally preferred for safety.

$string1 = "Hello 👨‍💻 World!";
// Using mb_substr ensures we get a whole character, not bytes
$string2 = mb_substr($string1, 0, 4); 

echo $string2; 
// Output: Hello (Wait, 'H','e','l','l' are 4 chars) 
// Actually let's try the emoji:
$string3 = "Hello 👨";
$trimmed = mb_substr($string3, 0, 5); // Returns 'Hello', skipping the complex emoji sequence if length allows

Another common pitfall involves handling strings that exceed the 100-character limit. If you are processing data from a database or user input where the field size is inconsistent, relying solely on substr without validation might lead to truncated data errors downstream. Always consider whether truncation is a loss of information you can accept. If the string is exactly 100 characters, substr returns it as-is; if it is less, it returns the whole thing. There is no need for conditional logic unless you specifically want to append an ellipsis (...) when the string exceeds the limit.

if (strlen($string1) > 100) {
    $string2 = substr($string1, 0, 97) . '...';
} else {
    $string2 = $string1;
}

For developers working within the Laravel ecosystem, you might encounter this requirement when preparing data for API responses or view rendering. While substr() works universally across all PHP environments, if you are building a package or a reusable utility class within a Laravel project, consider leveraging helper methods or creating a dedicated trait that encapsulates this logic with encoding checks and optional ellipsis appending. This pattern promotes cleaner code in your controllers and ensures consistency across different views.

Ultimately, the choice between substr() and mb_substr() depends on your data. For pure ASCII or strictly validated UTF-8 content where byte-level precision is acceptable, substr() remains the fastest and most memory-efficient option. For anything involving international characters or complex emoji sequences, mb_substr is the safer investment to prevent rendering artifacts.

Our network