ballhogjoni Posted August 26, 2008 Share Posted August 26, 2008 I have this url and I want to extract the number our of it. How would I do that? url: http://xxxxxxxxx.com/test-test1.html Link to comment https://forums.phpfreaks.com/topic/121405-solved-parse-number-from-url/ Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 The end of test-test? Will it always be test-test, or is that just an "example" query string that actually looks nothing like the real one? >_> If so, please show us the real string you're trying to parse so we can help better. Otherwise: <?php $str = "http://xxxxxx.com/test-test1.html"; $matches = array(); preg_match('~(test-test)([0-9]+?)\.html$~i', $str, $matches); echo $matches[2]; ?> Link to comment https://forums.phpfreaks.com/topic/121405-solved-parse-number-from-url/#findComment-626037 Share on other sites More sharing options...
ballhogjoni Posted August 26, 2008 Author Share Posted August 26, 2008 Test-test will not always be the same. Link to comment https://forums.phpfreaks.com/topic/121405-solved-parse-number-from-url/#findComment-626052 Share on other sites More sharing options...
DarkWater Posted August 26, 2008 Share Posted August 26, 2008 Will it always be in this format: http://something.com/something-something#.html ? Show me a few URLs that you're trying to parse and I'll write something up. Link to comment https://forums.phpfreaks.com/topic/121405-solved-parse-number-from-url/#findComment-626053 Share on other sites More sharing options...
ballhogjoni Posted August 26, 2008 Author Share Posted August 26, 2008 http://www.findacreditsolution.com/appliedto/balance-transfers1.html http://www.findacreditsolution.com/category/business10.html Link to comment https://forums.phpfreaks.com/topic/121405-solved-parse-number-from-url/#findComment-626059 Share on other sites More sharing options...
obsidian Posted August 26, 2008 Share Posted August 26, 2008 You can just match from the ending anchor: <?php if (preg_match('/([\d]+)\.[a-z]{3,4}$/', $url, $match)) { echo "Number is $match[1]"; } ?> Link to comment https://forums.phpfreaks.com/topic/121405-solved-parse-number-from-url/#findComment-626065 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.