Page 1 of 1
should i be using explode or split here?
Posted: Tue Jul 12, 2011 5:07 pm
by shawnku
heys guys can you tell me how to get whats between the "//" and the first "." in each url ?
the site is at this page :
http://shawn.sc2broadcast.net/test/
and the code i have is:
Code: Select all
<?php
$homepage = file_get_contents('http://www.macsiam.com/sites.php');
echo explode("//", $homepage);
?>
i would like each sub domain as a variable can you help me out? thanks.
Re: should i be using explode or split here?
Posted: Tue Jul 12, 2011 5:59 pm
by califdon
Use preg_match().
Re: should i be using explode or split here?
Posted: Tue Jul 12, 2011 7:53 pm
by twinedev
1. between explode and split, use explode as split is depreciated (being removed) as of PHP 5.3
2. Based upon the url you gave will always present the same format, just different URL's you can use the following:
Code: Select all
$homepage = file_get_contents('http://www.macsiam.com/sites.php');
preg_match_all('%\'http://([^./]+).[^.]+\.[a-z]{2,4}\'%i', $homepage, $domains, PREG_PATTERN_ORDER);
// $domains[0] is an array of matched full URLs (http://whatever.wherever.com/page.php)
// $domains[1] is an array of matched subdomains (whatever)
var_dump($domains[1]);
-Greg
Re: should i be using explode or split here?
Posted: Tue Jul 12, 2011 9:19 pm
by shawnku
hey thanks a lot greg. any way i can give you rep or something?
Re: should i be using explode or split here?
Posted: Tue Jul 12, 2011 10:26 pm
by twinedev
Small update, to allow for test.whatever.domain.com as well as just whatever.domain.com, get rid of the first period:
Code: Select all
$homepage = file_get_contents('http://www.macsiam.com/sites.php');
preg_match_all('%\'http://([^/]+).[^.]+\.[a-z]{2,4}\'%i', $homepage, $domains, PREG_PATTERN_ORDER);
// $domains[0] is an array of matched full URLs (http://whatever.wherever.com/page.php)
// $domains[1] is an array of matched subdomains (whatever)
var_dump($domains[1]);
Also, as mentioned the e-mail I sent if you want to have it accept both single and double quotes around the link:
Code: Select all
$homepage = file_get_contents('http://www.macsiam.com/sites.php');
preg_match_all('%\(\'|")http://([^/]+).[^.]+\.[a-z]{2,4}\1%i', $homepage, $domains, PREG_PATTERN_ORDER);
// $domains[0] is an array of matched full URLs (http://whatever.wherever.com/page.php)
// $domains[1] is an array of matched opening quote type (single or double) so it can be used as a reference at the close (\1)
// $domains[2] is an array of matched subdomains (whatever)
var_dump($domains[2]);