seany123 Posted June 13, 2011 Share Posted June 13, 2011 how can i strip an url to just leave the middle part. eg. http://www.google.com TO google Link to comment https://forums.phpfreaks.com/topic/239274-strip-url/ Share on other sites More sharing options...
AbraCadaver Posted June 13, 2011 Share Posted June 13, 2011 Are they always going to be like that or sometimes missing the http:// or maybe just http://google.com/etc...? Link to comment https://forums.phpfreaks.com/topic/239274-strip-url/#findComment-1229255 Share on other sites More sharing options...
seany123 Posted June 13, 2011 Author Share Posted June 13, 2011 they could be http://www.google.com http://google.com http://google.com/dfgjndfkgjndfg http://www.google.com/dijgndfgn so yeah anything to be honest Link to comment https://forums.phpfreaks.com/topic/239274-strip-url/#findComment-1229266 Share on other sites More sharing options...
jcbones Posted June 14, 2011 Share Posted June 14, 2011 See if this is something like you are wanting. RegEx Library <?php /*regEx pattern from Regular Expression Library */ $pat = '~^(?=[^&])(??<scheme>[^:/?#]+)?(?://(?<authority>[^/?#]*))?(?<path>[^?#]*)(?:\?(?<query>[^#]*))?(?:#(?<fragment>.*))?~'; /* array of sites, so that you can see the different returns */ $sites[] = 'http://www.google.com'; $sites[] = 'http://google.com'; $sites[] = 'http://google.com/dfgjndfkgjndfg'; $sites[] = 'http://www.google.com/dijgndfgn'; /* looping through the sites array */ foreach($sites as $str) { preg_match_all($pat,$str,$match); //match each site against the expression. $parts = explode('.',$match['authority'][0]); //grab the authority and split it on any period (.). foreach($parts as $value) {//cycle through the parts array created by explode(). if(strlen($value) < 4) { continue; } //if the value is less than 4 (minimum web address), then skip the next step. echo 'Website: ' . $value . '<br />'; //if it is greater than or equal to 4 chars. then echo it to the screen. } } ?> Link to comment https://forums.phpfreaks.com/topic/239274-strip-url/#findComment-1229363 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.