PHP Tutorial

PHP Flow Control

PHP Functions

PHP String

PHP Array

PHP Date Time

PHP Object Oriented

Regular Expression

PHP Cookie & Session

PHP Error & Exception handling

MySQL in PHP

PHP File Directory

PHP Image Processing

PHP substr() Function: Intercept String

The substr() function in PHP is used to extract a part of a string.

Here's the basic syntax of the substr() function:

substr(string, start, length)
  • string: The input string from which you want to extract a part.
  • start: The position from where to start extraction. If the start is a positive number, the start position will be that number of characters from the start of the string. If start is a negative number, then the start position will be that number of characters from the end of the string.
  • length (optional): The number of characters to extract. If not provided, the function extracts the remainder of the string.

Let's look at a few examples:

Example 1: Extracting from the beginning of the string

echo substr("Hello, world!", 0, 5); // Outputs: "Hello"

Example 2: Extracting from the end of the string

echo substr("Hello, world!", -1); // Outputs: "!"

Example 3: Extracting from a specific position

echo substr("Hello, world!", 7, 5); // Outputs: "world"

In the first example, we start from position 0 (the beginning of the string), and extract 5 characters, so we get "Hello".

In the second example, we start from position -1 (one character from the end of the string), and since we don't provide a length, we get the rest of the string from that position, which is "!".

In the third example, we start from position 7 and extract 5 characters, so we get "world".

Remember that string positions in PHP (and most programming languages) start from 0, not 1. So the first character of the string is at position 0.

  1. Extract substring in PHP:

    • Extracting a substring involves obtaining a portion of a string based on specified criteria, such as starting position and length.
    // File: extract_substring.php
    $originalString = "Hello, World!";
    $substring = substr($originalString, 7, 5);
    
    echo "Original: $originalString\nSubstring: $substring";
    // Output: Original: Hello, World!
    //         Substring: World
    
  2. PHP substr() example code:

    • The substr() function is commonly used to extract a substring from a string.
    // File: substr_example.php
    $originalString = "Programming";
    $substring = substr($originalString, 0, 7);
    
    echo "Original: $originalString\nSubstring: $substring";
    // Output: Original: Programming
    //         Substring: Program
    
  3. Get part of a string with substr() in PHP:

    • Use substr() to get a specified part of a string by providing the starting position and length.
    // File: get_part_with_substr.php
    $originalString = "PHP is powerful";
    $partOfString = substr($originalString, 0, 3);
    
    echo "Original: $originalString\nPart: $partOfString";
    // Output: Original: PHP is powerful
    //         Part: PHP
    
  4. Substring extraction in PHP with start and length:

    • Specify both the starting position and the length when extracting a substring using substr().
    // File: substring_with_start_and_length.php
    $originalString = "Welcome to PHP";
    $substring = substr($originalString, 11, 3);
    
    echo "Original: $originalString\nSubstring: $substring";
    // Output: Original: Welcome to PHP
    //         Substring: PHP
    
  5. Using substr() to slice strings in PHP:

    • substr() can be used to slice strings into desired segments.
    // File: slice_strings_with_substr.php
    $originalString = "Slicing strings in PHP";
    $firstSlice = substr($originalString, 0, 7);
    $secondSlice = substr($originalString, 8, 7);
    
    echo "Original: $originalString\nFirst Slice: $firstSlice\nSecond Slice: $secondSlice";
    // Output: Original: Slicing strings in PHP
    //         First Slice: Slicing
    //         Second Slice: strings
    
  6. PHP substr() vs substring() in other languages:

    • Note that in PHP, the function is called substr(), while in some other languages like JavaScript, it is often referred to as substring().
    // File: substr_vs_substring.php
    $originalString = "Language Comparison";
    $phpSubstring = substr($originalString, 0, 8);
    
    echo "Original: $originalString\nPHP Substring: $phpSubstring";
    // Output: Original: Language Comparison
    //         PHP Substring: Language
    
  7. Substring replacement using substr() in PHP:

    • substr() can be used for replacing parts of a string.
    // File: substring_replace_with_substr.php
    $originalString = "Replace me!";
    $replacement = "with PHP";
    $modifiedString = substr_replace($originalString, $replacement, 7);
    
    echo "Original: $originalString\nModified: $modifiedString";
    // Output: Original: Replace me!
    //         Modified: Replace with PHP
    
  8. Handling negative positions in substr() PHP:

    • Negative positions count from the end of the string.
    // File: negative_positions_substr.php
    $originalString = "Negative positions";
    $substring = substr($originalString, -9, 9);
    
    echo "Original: $originalString\nSubstring: $substring";
    // Output: Original: Negative positions
    //         Substring: positions