Home >Backend Development >PHP Tutorial >How to Delete Text After a Specific Substring in PHP?
Deleting Text After a Specific Substring
Removing text from a string after a particular substring is straightforward in PHP. Consider the example string:
Posted On April 6th By Some Dude
If you wish to delete all characters, including and following the substring "By", follow these steps:
<code class="php">$variable = substr($variable, 0, strpos($variable, "By"));</code>
This code employs the substr(), and strpos() functions:
Breaking this down in plain English:
Give me the portion of the string that starts from the beginning and ends at the point where you first encounter the substring "By".
Executing this code would result in:
Posted On April 6th
This method effectively removes everything after the specified substring.
The above is the detailed content of How to Delete Text After a Specific Substring in PHP?. For more information, please follow other related articles on the PHP Chinese website!