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 "-".
Use the String. slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); .
The trim() function removes whitespace and other predefined characters from both sides of a string. Related functions: ltrim() - Removes whitespace or other predefined characters from the left side of a string. rtrim() - Removes whitespace or other predefined characters from the right side of a string.
To get the substring after a specific character, call the substring() method, passing it the index after the character's index as a parameter. The substring method will return the part of the string after the specified character.
You can use strstr to do this. Show activity on this post. The explode is in fact a better answer, as the question was about removing the text before the string.
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));
How about some explosions:
$parts = explode( '-', "Today is - Friday and tomorrow is - Saturday" );
echo $parts[0].'-'.$parts[1];
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With