I'm just wondering how I could remove everything after a certain substring in PHP
ex:
Posted On April 6th By Some Dude
I'd like to have it so that it removes all the text including, and after, the sub string "By"
Thanks
I'm just wondering how I could remove everything after a certain substring in PHP
ex:
Posted On April 6th By Some Dude
I'd like to have it so that it removes all the text including, and after, the sub string "By"
Thanks
Below is the most efficient method (by run-time) to cut off everything after the first By in a string. If By does not exist, the full string is returned. The result is in $sResult.
If you don't want to be case sensitive, use stripos instead of strpos. If you think By might exist more than once and want to cut everything after the last occurrence, use strrpos.
Below is a less efficient method but it takes up less code space. This method is also more flexible and allows you to do any regular expression.
For example, if you wanted to remove everything after the day:
For case insensitive, add the i modifier like this:
To get everything past the last By if you think there might be more than one, add an extra .* at the beginning like this:
But here is also a really powerful way you can use preg_match to do what you may be trying to do:
Regular expressions might seem confusing at first but they can be really powerful and your best friend once you master them! Good luck!
One method would be: