Jump to content

Removing end of string after ...


Mcod

Recommended Posts

Hello,

 

I am currently having an issue with removing a string from within a string.

 

The strings I deal with are like the examples below:

 

This is - a test - because tests are fun - abc

This is - a test because tests are fun - def

This is a test - because tests are fun - ghi

 

The good news is that the end is always the same - basically a space, a minus and a space followed by random chars.

 

So technically I would need to remove everything from a string starting at the last space minus space incl the space, minus, space.

 

The result for the examples above would then be:

 

This is - a test - because tests are fun

This is - a test because tests are fun

This is a test - because tests are fun

 

Would be great if somebody could help me with this (to me) impossible task.

 

Thanks :)

Link to comment
https://forums.phpfreaks.com/topic/246421-removing-end-of-string-after/
Share on other sites

If it's always the characters including and after the last hyphen that need to be removed, you don't even need to use a regex pattern for it.

 

$string = trim(substr( $string, 0, strrpos($string, '-') ));

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.