Home > Article > Backend Development > How to Remove Text After a Specific Substring in PHP?
Deleting Data from a String After a Specific Point
In PHP, eliminating specific sections of a string after a particular substring can be a common task. Let's dive into this scenario:
You have a string containing information separated by subtexts, such as "Posted On April 6th By Some Dude." Your goal is to selectively remove everything, including the substring "By," from this string.
To achieve this, utilize the following code:
$variable = substr($variable, 0, strpos($variable, "By"));
This code effectively slices the string into two parts:
Combining these actions, the code isolates the part of the string before the "By" substring, effectively stripping the designated portion from the original string.
The above is the detailed content of How to Remove Text After a Specific Substring in PHP?. For more information, please follow other related articles on the PHP Chinese website!