Removing characters after last alpha character (if exist) in url in PHP
up vote
0
down vote
favorite
I'm redoing a website and on this website there are two types of URL such as:
http://www.example.com/category/subcategory/one-friend-url-040569485.php
http://www.example.com/category/subcategory/one-friend-url.php
I need to get only one-friend-url without the extension .php or -040569485.php in any of the above situations, so I can submit the one-friend-url to be searched in mySQL.
So if the URL does not have a -040569485.php it just removes the .php extension, otherwise removes the -040569485.php from the URL.
What would be the best way to do this (php, regex or .htaccess)?
php regex .htaccess
add a comment |
up vote
0
down vote
favorite
I'm redoing a website and on this website there are two types of URL such as:
http://www.example.com/category/subcategory/one-friend-url-040569485.php
http://www.example.com/category/subcategory/one-friend-url.php
I need to get only one-friend-url without the extension .php or -040569485.php in any of the above situations, so I can submit the one-friend-url to be searched in mySQL.
So if the URL does not have a -040569485.php it just removes the .php extension, otherwise removes the -040569485.php from the URL.
What would be the best way to do this (php, regex or .htaccess)?
php regex .htaccess
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I'm redoing a website and on this website there are two types of URL such as:
http://www.example.com/category/subcategory/one-friend-url-040569485.php
http://www.example.com/category/subcategory/one-friend-url.php
I need to get only one-friend-url without the extension .php or -040569485.php in any of the above situations, so I can submit the one-friend-url to be searched in mySQL.
So if the URL does not have a -040569485.php it just removes the .php extension, otherwise removes the -040569485.php from the URL.
What would be the best way to do this (php, regex or .htaccess)?
php regex .htaccess
I'm redoing a website and on this website there are two types of URL such as:
http://www.example.com/category/subcategory/one-friend-url-040569485.php
http://www.example.com/category/subcategory/one-friend-url.php
I need to get only one-friend-url without the extension .php or -040569485.php in any of the above situations, so I can submit the one-friend-url to be searched in mySQL.
So if the URL does not have a -040569485.php it just removes the .php extension, otherwise removes the -040569485.php from the URL.
What would be the best way to do this (php, regex or .htaccess)?
php regex .htaccess
php regex .htaccess
edited Nov 8 at 23:27
MrWhite
11.9k33059
11.9k33059
asked Nov 8 at 22:09
Web User
1305
1305
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
accepted
Can you try the regex if you need it in the format between "subcategory/" and ".php" with the numbers as optional
"subcategory/(.*?)(?:-?d*?).php"

It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
Can you try the regex if you need it in the format between "subcategory/" and ".php" with the numbers as optional
"subcategory/(.*?)(?:-?d*?).php"

It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
add a comment |
up vote
1
down vote
accepted
Can you try the regex if you need it in the format between "subcategory/" and ".php" with the numbers as optional
"subcategory/(.*?)(?:-?d*?).php"

It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
Can you try the regex if you need it in the format between "subcategory/" and ".php" with the numbers as optional
"subcategory/(.*?)(?:-?d*?).php"

Can you try the regex if you need it in the format between "subcategory/" and ".php" with the numbers as optional
"subcategory/(.*?)(?:-?d*?).php"

edited Nov 8 at 23:06
answered Nov 8 at 22:49
Ashok KS
203214
203214
It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
add a comment |
It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
It worked perfectly, just had to add a "" escape delimiter: "subcategory/(.*?)(?:-?d*?).php". When testing I came across a third possible URL, which could come without the ".php" extension previously removed by the htacess, in which case it would then have to leave the ".php" extension as optional. The final regex looks like this:"subcategory/(.*?)(?:-?d*?)?(.php)?$"
– Web User
Nov 9 at 7:22
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53216900%2fremoving-characters-after-last-alpha-character-if-exist-in-url-in-php%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown