samirah samirah - 1 year ago 128
PHP Question

Strip everything in string after the second "-" character that occurs?

How can i strip everything in a string after the character "-" has occurred for the second time?

For example: Today is - Friday and tomorrow is - Saturday

In this case i would want Saturday to be removed along with the last - so somehow strip : "- Saturday"

Any help is very much appreciated :) I can only seem to get everything to be removed after the first "-".

Answer Source

Use strpos to find the first occurrence and use it again to find the point to end using the offset option with the value from previous. Then use substr.

$newstr = substr($str, 0, strpos($str, '-', strpos($str, '-')+1));
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download