How to Remove String Portions After a Specified Substring in PHP
When dealing with strings, it often becomes necessary to remove specific portions based on certain criteria. This can be achieved in PHP using the substr() function.
Use Case:
Consider a situation where you have a string containing additional information you wish to remove, such as:
Posted On April 6th By Some Dude
You want to remove the text following the substring "By."
Solution:
Utilizing substr(), you can selectively retrieve the desired portion of the string:
<code class="php">$variable = substr($variable, 0, strpos($variable, "By"));</code>
Explanation:
Now, $variable will contain the following:
Posted On April 6th
The above is the detailed content of How to Remove String Portions After a Specific Substring in PHP?. For more information, please follow other related articles on the PHP Chinese website!