imperialized Posted February 7, 2012 Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/ Share on other sites More sharing options...
ragax Posted February 7, 2012 Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315381 Share on other sites More sharing options...
imperialized Posted February 7, 2012 Author Share Posted February 7, 2012 That looks like it should accomplish EXACTLY what I need. Thank you for the exceptionally quick response!! Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315383 Share on other sites More sharing options...
ragax Posted February 7, 2012 Share Posted February 7, 2012 It's a pleasure. You have to work fast when dinner is served. Going offline now, but if the expression gives you any trouble I'm sure someone on the board will be able to help. Wishing you a fun day Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315384 Share on other sites More sharing options...
imperialized Posted February 7, 2012 Author Share Posted February 7, 2012 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! Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315385 Share on other sites More sharing options...
ragax Posted February 7, 2012 Share Posted February 7, 2012 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. Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315387 Share on other sites More sharing options...
silkfire Posted February 7, 2012 Share Posted February 7, 2012 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315427 Share on other sites More sharing options...
abareplace Posted February 7, 2012 Share Posted February 7, 2012 Silkfire, the last star (*) is unnecessary: ^(\d+)\D*(\d+) instead of ^(\d+)[^\d]*(\d+)* Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315439 Share on other sites More sharing options...
ragax Posted February 7, 2012 Share Posted February 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315559 Share on other sites More sharing options...
silkfire Posted February 7, 2012 Share Posted February 7, 2012 Wow you're playful even in your posts =) Ye I love explode + array_pop or _shift one of the best functions in PHP =P Quote Link to comment https://forums.phpfreaks.com/topic/256589-pulling-category-id-from-url/#findComment-1315583 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.