Avatar of Chris Andrews
Chris Andrews
Flag for United States of America asked on

need regex help to remove numbers and trailing slash

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
Regular ExpressionsPHP

Avatar of undefined
Last Comment
Chris Andrews

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Chris Andrews

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Chris Stanyon

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.
Chris Stanyon

Your solution won't give you the trailing slash !!
Chris Andrews

ASKER
Got it Chris, thank you!
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes