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
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
.
Extract substring in PHP:
// File: extract_substring.php $originalString = "Hello, World!"; $substring = substr($originalString, 7, 5); echo "Original: $originalString\nSubstring: $substring"; // Output: Original: Hello, World! // Substring: World
PHP substr()
example code:
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
Get part of a string with substr()
in PHP:
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
Substring extraction in PHP with start and length:
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
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
PHP substr()
vs substring()
in other languages:
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
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
Handling negative positions in substr()
PHP:
// File: negative_positions_substr.php $originalString = "Negative positions"; $substring = substr($originalString, -9, 9); echo "Original: $originalString\nSubstring: $substring"; // Output: Original: Negative positions // Substring: positions