puretruth Posted January 12, 2012 Share Posted January 12, 2012 I have a URL: /reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731 I need a regex that returns the folder (/reserve-unit/) and the location ID (loc=1000000174) and the word "complete." Everything else can be ignored. Thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/ Share on other sites More sharing options...
puretruth Posted January 12, 2012 Author Share Posted January 12, 2012 More info: the URL will always be in this format. The strings will always be the same in length. Basically we are tracking a self-storage facility unit rentals by location. That's why I need "loc" and "complete." The "thank you" page has "complete" in the URL string. The other values just determine the unit type and transaction ID. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307007 Share on other sites More sharing options...
Pikachu2000 Posted January 12, 2012 Share Posted January 12, 2012 How about parse_url? Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307014 Share on other sites More sharing options...
puretruth Posted January 12, 2012 Author Share Posted January 12, 2012 How about parse_url? Thanks for the reply. I am passing this info along to my friend. See where it gets him. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307018 Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 Fantastic. I love Pikachu's simple yet efficient idea. Once you've used parse_url, you'll need code to extract what you want from the array. As an alternate solution, in case you'd like to do it with regex, try this code. If I've understood your request, it should do what you want. Input: /reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731 Code: <?php $pattern=',/([^/]+)/.*?(loc=[^&]+)&[^&]+&([\w]+)=,'; $subject='/reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731'; preg_match($pattern,$subject,$match); for($i=1;$i<count($match);$i++) // or in this case, 4 instead of count($match) echo $match[$i].'<br />'; ?> Output: reserve-unit loc=1000000174 complete Please let me know if you'd like further details. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307035 Share on other sites More sharing options...
puretruth Posted January 12, 2012 Author Share Posted January 12, 2012 Fantastic. I love Pikachu's simple yet efficient idea. Once you've used parse_url, you'll need code to extract what you want from the array. As an alternate solution, in case you'd like to do it with regex, try this code. If I've understood your request, it should do what you want. Input: /reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731 Code: <?php $pattern=',/([^/]+)/.*?(loc=[^&]+)&[^&]+&([\w]+)=,'; $subject='/reserve-unit/?unit=1000123801&loc=1000000174&transaction=1002731154&complete=1000706731'; preg_match($pattern,$subject,$match); for($i=1;$i<count($match);$i++) // or in this case, 4 instead of count($match) echo $match[$i].'<br />'; ?> Output: reserve-unit loc=1000000174 complete Please let me know if you'd like further details. I think this is exactly what he was looking for. Thank you very much. I will post once it has been confirmed. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307039 Share on other sites More sharing options...
ragax Posted January 12, 2012 Share Posted January 12, 2012 I think this is exactly what he was looking for. Thank you very much. I will post once it has been confirmed. Fantastic. Yes, please let me know if it works on real data / needs tweaking. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307042 Share on other sites More sharing options...
puretruth Posted January 13, 2012 Author Share Posted January 13, 2012 It works so far perfectly. Thank you for the help. It is appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307370 Share on other sites More sharing options...
ragax Posted January 13, 2012 Share Posted January 13, 2012 And thank you for your courtesy! It makes answering questions that much more pleasurable when the person who asks is as courteous as you. Wishing you a fun weekend. Quote Link to comment https://forums.phpfreaks.com/topic/254899-parse-a-url/#findComment-1307374 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.