should i be using explode or split here?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
shawnku
Forum Newbie
Posts: 3
Joined: Wed Aug 05, 2009 5:40 pm

should i be using explode or split here?

Post 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.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: should i be using explode or split here?

Post by califdon »

Use preg_match().
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: should i be using explode or split here?

Post 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
shawnku
Forum Newbie
Posts: 3
Joined: Wed Aug 05, 2009 5:40 pm

Re: should i be using explode or split here?

Post by shawnku »

hey thanks a lot greg. any way i can give you rep or something?
User avatar
twinedev
Forum Regular
Posts: 984
Joined: Tue Sep 28, 2010 11:41 am
Location: Columbus, Ohio

Re: should i be using explode or split here?

Post 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]);
Post Reply