Jeffro Posted August 3, 2011 Share Posted August 3, 2011 I need a php fix that says... No matter what the phrase (because it will always be different), delete the last 3 dashes and the last 3 words. This-is-an-example-sentence-right-here would output: This-is-an-example Possible.. or too tricky? Thanks! Link to comment https://forums.phpfreaks.com/topic/243676-how-do-i-chop-the-end-off-of-these-phrases/ Share on other sites More sharing options...
Alex Posted August 3, 2011 Share Posted August 3, 2011 You don't need regex for this. You can just use explode: $str = 'This-is-an-example-sentence-right-here'; echo implode('-', explode('-', $str, -3)); Note: This requires PHP 5.1.0 (when support for a negative limit parameter was added). Link to comment https://forums.phpfreaks.com/topic/243676-how-do-i-chop-the-end-off-of-these-phrases/#findComment-1251115 Share on other sites More sharing options...
Jeffro Posted August 3, 2011 Author Share Posted August 3, 2011 You don't need regex for this. You can just use explode: $str = 'This-is-an-example-sentence-right-here'; echo implode('-', explode('-', $str, -3)); Note: This requires PHP 5.1.0 (when support for a negative limit parameter was added). That could not have worked any better. I expected a much longer solution. THANK YOU!!! Link to comment https://forums.phpfreaks.com/topic/243676-how-do-i-chop-the-end-off-of-these-phrases/#findComment-1251419 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.