Solarpitch Posted September 8, 2010 Share Posted September 8, 2010 Hey guys, I have a url that looks like this http://www.mysite.com/our-company-t-2.html I need to extract the number at the end. It's 2 here but could be 2478 for example. Just wondering how I can do this. Would it need to do something like look for anything between "-" and ".html" ? Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/ Share on other sites More sharing options...
AbraCadaver Posted September 8, 2010 Share Posted September 8, 2010 Is this the URL of the page that is running or are you getting this from somewhere else? If you are getting it from somewhere else then something like: preg_match('/([\d]+)\.html$/', $url, $matches); print_r($matches[1]); Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108850 Share on other sites More sharing options...
unknowncat Posted September 8, 2010 Share Posted September 8, 2010 If this is the model for urls you're wanting to use on your site (i see you used 'mysite.com' in your example) then you can have your server interpret all such urls into GET variables using .htaccess file and rewrite rules. Depending on what you want to name your get vars, here is an example: Options +FollowSymLinks RewriteEngine on RewriteRule our-company-(.*)-(.*)\.html our-company.html?letter=$1&number=$2 Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108856 Share on other sites More sharing options...
Solarpitch Posted September 8, 2010 Author Share Posted September 8, 2010 Is this the URL of the page that is running or are you getting this from somewhere else? If you are getting it from somewhere else then something like: preg_match('/([\d]+)\.html$/', $url, $matches); print_r($matches[1]); its the url that the page is running.. should the above work with that also? Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108859 Share on other sites More sharing options...
AbraCadaver Posted September 8, 2010 Share Posted September 8, 2010 As unknowncat states, it may already be rewritten by apache into a usable get string. Do you actually have a physical file on your server called our-company-t-2.html? Try this to see what's available: print_r($_GET); Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108863 Share on other sites More sharing options...
Solarpitch Posted September 8, 2010 Author Share Posted September 8, 2010 As unknowncat states, it may already be rewritten by apache into a usable get string. Do you actually have a physical file on your server called our-company-t-2.html? Try this to see what's available: print_r($_GET); no.. that html is dynamically generated. Thats the problem. I should have stated that. Sorry Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108879 Share on other sites More sharing options...
AbraCadaver Posted September 8, 2010 Share Posted September 8, 2010 Try this to see what's available: print_r($_GET); And??????? Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108898 Share on other sites More sharing options...
Psycho Posted September 8, 2010 Share Posted September 8, 2010 If it is the page that is running then, most likely you can't use PHP anyway since it is an html page not a php page. Of course, it is possible the server is configured to process html pages for php code anyway, although it is not common that I am aware. So, on any page where PHP is interpreted you could do this: preg_match('/([\d]+)\..{3,4}(\?.*)+$/', $_SERVER['REQUEST_URI'], $match); $number = $match[1]; I modified the regex expression provided by Solarpitch to allow for pages with different extensions: html, htm, php, etc. as long as they are 3 or 4 characters. Also, the URL may optionally have additional parameters, such as http://www.mysite.com/our-company-t-2.html?id=34&cat=6 Quote Link to comment https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/#findComment-1108907 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.