Chicken Posted July 19, 2007 Share Posted July 19, 2007 I need to retrieve a 7 digit from an address where there is not set start/end of the numbers ex: http://site.com/something/Variable_length/numberhere/varialble_length Link to comment https://forums.phpfreaks.com/topic/60810-getting-a-number-from-an-address/ Share on other sites More sharing options...
akitchin Posted July 19, 2007 Share Posted July 19, 2007 one thing you could do is explode the URL using forward slashes into a series of array items, or use parse_url(). note that parse_url() may fail with deformed URLs, so it may be less reliable. when exploding, you could simply search until you find something numeric and 7 characters long. if you KNOW that the 7-digit number will reside in the same spot in the path everytime, you don't even need to search: $url = 'http://site.com/something/Variable_length/numberhere/varialble_length'; $pieces = explode('/', $url); // if you KNOW the format of the URL will always be the same: echo 'number is: '.$pieces[4]; // if you don't necessarily know that the number will be there: foreach ($pieces AS $piece) { if (is_numeric($piece) && strlen($piece) == 7) { echo 'the number is '.$piece.', i THINK.'; } } Link to comment https://forums.phpfreaks.com/topic/60810-getting-a-number-from-an-address/#findComment-302536 Share on other sites More sharing options...
Chicken Posted July 19, 2007 Author Share Posted July 19, 2007 Thanks that worked Link to comment https://forums.phpfreaks.com/topic/60810-getting-a-number-from-an-address/#findComment-302668 Share on other sites More sharing options...
Chicken Posted July 19, 2007 Author Share Posted July 19, 2007 I've got another problem about the same thing except this time i need to get an alphanumeric string of from the end of a php example: htp://www.place.com/blah?v=sRv953XZX6Y the length of the alphanumeric string is variable but the "htp://www.place.com/blah?v=" remains the same length Link to comment https://forums.phpfreaks.com/topic/60810-getting-a-number-from-an-address/#findComment-302890 Share on other sites More sharing options...
akitchin Posted July 20, 2007 Share Posted July 20, 2007 then you can use parse_url(), grabbing the 'query' portion: $url = 'http://www.place.com/blah?v=sRv953XZX6Y'; $components = parse_url($url); $query = explode('=', $components['query']); echo 'value of '.$query[0].' is '.$query[1]; this is far from ideal, but a possible solution. Link to comment https://forums.phpfreaks.com/topic/60810-getting-a-number-from-an-address/#findComment-303658 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.