Jump to content

Recommended Posts

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" ?

Link to comment
https://forums.phpfreaks.com/topic/212894-picking-out-number-from-a-url-string/
Share on other sites

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]);

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

 

 

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?

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);

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.