need regex help to remove numbers and trailing slash

St_Aug_Beach_Bum
St_Aug_Beach_Bum used Ask the Experts™
on
I need some coding help... I have a string, for example:

$string = "post-title/367895378/";

The numbers there... in the string they always start with 367 and then there are anywhere from 3 to 6 digits afterwards.

I need to remove the "367" and all the numbers afterwards, and the trailing slant, so that the string ends up as "post-title/" (the post title changes based on the page it's on, of course).

The numbers starting with 367... and the slant are always going to be at the end of the url, so everything starting with 367 and afterwards should be eliminated.

I know that needs some kind of regex, but that's beyond my skills...

Thanks,

Chris
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Got it, regex not needed....


echo strstr($string, 'By', true) ?: $string;
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
If your rules really are as simple as that, then you don't really need a regex. Just search for the first / and substring on that:

$string = "post-title/367895378/";
$result = substr($string, 0, strpos($string, "/") + 1);

Open in new window

If your rules are more complicated, then you might want to explain all possible variations of $string.
Most Valuable Expert 2018
Distinguished Expert 2018

Commented:
Your solution won't give you the trailing slash !!

Author

Commented:
Got it Chris, thank you!

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial