Link to home
Create AccountLog in
Avatar of Chris Andrews
Chris AndrewsFlag 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...


Avatar of Chris Andrews
Chris Andrews
Flag of United States of America image

Link to home
Create an account to see this answer
Signing up is free. No credit card required.
Create Account
Avatar of 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.
Your solution won't give you the trailing slash !!
Avatar of Chris Andrews


Got it Chris, thank you!