I'm looking for a non-regex solution (if possible) to the following problem:
I'd like to remove everything up to and including a particular string within a string.
So, for example, £10.00 - £20.00
becomes just £20.00
, maybe by providing the function with -
as a parameter.
I've tried strstr
and ltrim
, but neither were quite what I was after.
This can be achieved using string manipulation functions in PHP. First we find the position of the
-
character in the string usingstrpos()
. Usesubstr()
to get everything until that character (including that one). Then usetrim()
to remove whitespace from the beginning and/or end of the string:Alternatively, you could split the string into two pieces, with
-
as the delimiter, and take the second part:This could be done in many different ways. In the end, it all boils down to your preferences and requirements.