Jump to content

Pulling Category ID from URL


imperialized

Recommended Posts

This sounds much more simple than it really is.. to me anyhow! I really don't have a true understanding off all this Regex stuff so I really hope someone here can give me a hand!

 

I'm working with a Zen Cart installation with SEO URLs installed so all of the pages are cached .html files.

 

The URLS have the following (patterns?)

http://domain.com/xxx-c-39.html

http://domain.com/xxx-c-39_107.html

 

The number that I need from that URL would be "39"

 

Is there a way to just parse the URL and pull that number? it always immediately follows the last - but the length of the category ID changes.

 

Any help is appreciated!!! Thanks in advance.

Link to comment
Share on other sites

Here you go, bro.

 

Input:

http://domain.com/xxx-c-39.html

http://domain.com/xxx-c-38_107.html

 

Code:

<?php
$s1='http://domain.com/xxx-c-39.html';
$s2='http://domain.com/xxx-c-38_107.html';
$regex=',http://domain\.com/(?:[[:alpha:]]+-){2}+(\d+),';
preg_match($regex,$s1,$m);
echo $m[1].'<br />';
preg_match($regex,$s2,$m);
echo $m[1].'<br />';
?>

 

Output:

39

38

 

Let me know if this is what you need. ;)

Link to comment
Share on other sites

Thank you, I did just realize however that I need to grab the following number if it is available as well. Not quite as important, but can come in handy for me. If I had any clue how regex worked I could figure it out by looking at the code you provided. Unfortunately, I don't have the slightest clue!

 

If anyone here could point me in the right direction,

 

http://domain.com/xxx-c-39_107.html

 

I am referring to pulling the 107 from the URL. Again, this number could be more or less than 3 characters.

 

Thanks!

Link to comment
Share on other sites

Code:

<?php
$s1='http://domain.com/xxx-c-39.html';
$s2='http://domain.com/xxx-c-38_107.html';
$regex=',http://domain\.com/(?:[[:alpha:]]+-){2}+(\d+)(?:_(\d+))?,';
preg_match($regex,$s1,$m);
echo $m[1].'<br />';
preg_match($regex,$s2,$m);
echo $m[1].'<br />';
echo $m[2].'<br />';
?>

 

 

Output:

39

38

107

 

Edit to explain a bit: This assumes an underscore between the two numbers. If the separator can be something else, such as a dash, let us know, you'll have to replace the underscore with something like [-_], where the [brackets] contain the allowed separators. In general, if there is anything "fixed" in the regex (e.g. the domain name, the dashes) that you want to make variable, just let us know. :)

 

Link to comment
Share on other sites

Scriptwise, you can shorten the code by knowing that you're only interested in the portion after the last hyphen (-).

 

$s1 = 'http://domain.com/xxx-c-39.html';
$s2 = 'http://domain.com/xxx-c-38_107.html';

preg_match('#^(\d+)[^\d]*(\d+)*#', array_pop(explode('-', $s1)), $id);
echo $id[1] . (isset($id[2]) ? ', ' . $id[2]) . '<br>';

preg_match('#^(\d+)[^\d]*(\d+)*#', array_pop(explode('-', $s2)), $id);
echo $id[1] . (isset($id[2]) ? ', ' . $id[2]) . '<br>';

Link to comment
Share on other sites

Hi Silkfire, hi abareplace!

 

Silkfire, that's a really fun trick, thank you for sharing it. :)

Exploring your idea, I'm sure you know this, but for the "knowledge base", your

array_pop(explode('-', $s1)), $id)

 

can be accomplished in regex. You would use something like this at the beginning of the pattern, making the regex eat up all sequences of non-dashes followed by a dash.

(?:[^-]*+-)*+

 

In the original code, this would give us:

<?php
$s1='http://domain.com/xxx-c-39.html';
$s2='http://domain.com/xxx-c-38_107.html';
$regex=',(?:[^-]*+-)*+(\d+)(?:_(\d+))?,';
preg_match($regex,$s1,$m);
echo $m[1].'<br />';
preg_match($regex,$s2,$m);
echo $m[1].'<br />';
echo $m[2].'<br />';
?>

 

A few considerations for the knowledge base:

- If you're using this idea (either in this format or Silkfire's), you must know in advance that the stem of the string contains the right domain (as it is no longer validated by the regex).

- For the array version, you may need an ini_set('display_errors', 0); as PHP 5.3.8 complains like so: "Strict Standards: Only variables should be passed by reference".

- The array version will take twice as long to run, but you'll only notice if you run it 100,000 times.

 

Silkfire, thank you for the stimulating idea, it's fun to explore options.

 

Wishing you all a fun day

Link to comment
Share on other sites

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.