MyBB Community Forums

Full Version: Using PHP Explode and If Else
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I want to set it so that I can check a URL for a certain extension (in specified list) and return results based on whether it appears or not.

I know I have to explode the following (I know that most of the code is wrong, but I'm posting it so that you can get the gist of what I want):

$url = "http://www.test.com";
$old_ext = ".com,.net,.org,.info";
$ext = explode(",", "$old_ext");

$check = stristr("$url","$ext");
if ($check == '$ext') {
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}

Regardless, is there an easier way? Could I use a foreach statement instead maybe?

Edit: Wrong forum, please move to General Chat.
(2009-09-27, 03:28 PM)Zash Wrote: [ -> ]I want to set it so that I can check a URL for a certain extension (in specified list) and return results based on whether it appears or not.

I know I have to explode the following (I know that most of the code is wrong, but I'm posting it so that you can get the gist of what I want):

$url = "http://www.test.com";
$old_ext = ".com,.net,.org,.info";
$ext = explode(",", "$old_ext");

$check = stristr("$url","$ext");
if ($check == '$ext') {
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}

Regardless, is there an easier way? Could I use a foreach statement instead maybe?

Edit: Wrong forum, please move to General Chat.

WWWHostingServer.com technical support is coming to your rescue again! Wink

To be fail safe your code should be like this :

$ext_array = array(".com",".net",".org",".info");
$url_array = parse_url($URL);
$last_ext = strrchr($url_array['host'],'.');
if (in_array($last_ext,$ext_array)){
  echo "Domain has allowed extension.";
}
else {
  echo "Domain does not have allowed extension";
}
(2009-09-27, 04:24 PM)exdiogene Wrote: [ -> ]
(2009-09-27, 03:28 PM)Zash Wrote: [ -> ]I want to set it so that I can check a URL for a certain extension (in specified list) and return results based on whether it appears or not.

I know I have to explode the following (I know that most of the code is wrong, but I'm posting it so that you can get the gist of what I want):

$url = "http://www.test.com";
$old_ext = ".com,.net,.org,.info";
$ext = explode(",", "$old_ext");

$check = stristr("$url","$ext");
if ($check == '$ext') {
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}

Regardless, is there an easier way? Could I use a foreach statement instead maybe?

Edit: Wrong forum, please move to General Chat.

WWWHostingServer.com technical support is coming to your rescue again! Wink

To be fail safe your code should be like this :

$ext_array = array(".com",".net",".org",".info");
$url_array = parse_url($URL);
$last_ext = strrchr($url_array['host'],'.');
if (in_array($last_ext,$ext_array)){
  echo "Domain has allowed extension.";
}
else {
  echo "Domain does not have allowed extension";
}
Thanks Smile I noticed one thing in your code:
$last_ext = strrchr($url_array['host'],'.');

The only problem with this is that it won't work for country TLDs such as co.uk, which I do plan on allowing.
(2009-09-27, 04:25 PM)Zash Wrote: [ -> ]
(2009-09-27, 04:24 PM)exdiogene Wrote: [ -> ]
(2009-09-27, 03:28 PM)Zash Wrote: [ -> ]I want to set it so that I can check a URL for a certain extension (in specified list) and return results based on whether it appears or not.

I know I have to explode the following (I know that most of the code is wrong, but I'm posting it so that you can get the gist of what I want):

$url = "http://www.test.com";
$old_ext = ".com,.net,.org,.info";
$ext = explode(",", "$old_ext");

$check = stristr("$url","$ext");
if ($check == '$ext') {
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}

Regardless, is there an easier way? Could I use a foreach statement instead maybe?

Edit: Wrong forum, please move to General Chat.

WWWHostingServer.com technical support is coming to your rescue again! Wink

To be fail safe your code should be like this :

$ext_array = array(".com",".net",".org",".info");
$url_array = parse_url($URL);
$last_ext = strrchr($url_array['host'],'.');
if (in_array($last_ext,$ext_array)){
  echo "Domain has allowed extension.";
}
else {
  echo "Domain does not have allowed extension";
}
Thanks Smile I noticed one thing in your code:
$last_ext = strrchr($url_array['host'],'.');

The only problem with this is that it won't work for country TLDs such as co.uk, which I do plan on allowing.

I did it this way because you did not mentioned any extensions like those in your first post. But you still can include ".uk" in your array.

The problem with checking the first "." is that it will give problems with subdomains. The only other solution would be to verify the end of the hostname against all possible extensions in the array in a for-next loop.
(2009-09-27, 04:32 PM)exdiogene Wrote: [ -> ]I did it this way because you did not mentioned any extensions like those in your first post. But you still can include ".uk" in your array.

Thanks, but if I added things like .cc then it would allow co.cc domains and stuff I don't want.

Hmm, what if a check was done for the amount of . and if it was 2 then the snippet would scan a different set of TLDs?

Something like:

$count = substr_count($url,".");

if ( $count == "1" ) {
$ext_array = array(".com",".net",".org",".info");
$url_array = parse_url($URL);
$last_ext = strrchr($url_array['host'],'.');
if (in_array($last_ext,$ext_array)){
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}
}
else {
$ext_array = array(".co.uk",".org.uk");
$url_array = parse_url($URL);
(Some code to check extension starting the second-to-last .)
if (in_array($last_ext,$ext_array)){
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}
}

Edit: Found another problem, adding or removing www. would effect the code as well.
(2009-09-27, 04:43 PM)Zash Wrote: [ -> ]
(2009-09-27, 04:32 PM)exdiogene Wrote: [ -> ]
(2009-09-27, 04:25 PM)Zash Wrote: [ -> ]
(2009-09-27, 04:24 PM)exdiogene Wrote: [ -> ]
(2009-09-27, 03:28 PM)Zash Wrote: [ -> ]I want to set it so that I can check a URL for a certain extension (in specified list) and return results based on whether it appears or not.

I know I have to explode the following (I know that most of the code is wrong, but I'm posting it so that you can get the gist of what I want):

$url = "http://www.test.com";
$old_ext = ".com,.net,.org,.info";
$ext = explode(",", "$old_ext");

$check = stristr("$url","$ext");
if ($check == '$ext') {
echo "Domain has allowed extension.";
}
else {
echo "Domain does not have allowed extension";
}

Regardless, is there an easier way? Could I use a foreach statement instead maybe?

Edit: Wrong forum, please move to General Chat.

WWWHostingServer.com technical support is coming to your rescue again! Wink

To be fail safe your code should be like this :

$ext_array = array(".com",".net",".org",".info");
$url_array = parse_url($URL);
$last_ext = strrchr($url_array['host'],'.');
if (in_array($last_ext,$ext_array)){
  echo "Domain has allowed extension.";
}
else {
  echo "Domain does not have allowed extension";
}
Thanks Smile I noticed one thing in your code:
$last_ext = strrchr($url_array['host'],'.');

The only problem with this is that it won't work for country TLDs such as co.uk, which I do plan on allowing.

I did it this way because you did not mentioned any extensions like those in your first post. But you still can include ".uk" in your array.
Thanks, but if I added things like .cc then it would allow co.cc domains and stuff I don't want.

Hmm, what if a check was done for the amount of . and if it was 3 then the snippet would scan a different set of TLDs?

It would be more efficient to scan the end of the hostname in this case like this :

$ext_array = array(".com",".net",".org",".info",".co.uk");
$url_array = parse_url($URL);
$found = false;
foreach($ext_array as $ext){
  if (substr($url_array['host'],-strlen($ext)) == $ext){
    $found = true;
    echo "Domain has allowed extension.";
    break;
  }
}
if (!$found){
  echo "Domain does not have allowed extension";
}
Works like a charm Smile Thanks a lot.

As you have noticed, I'm learning a lot about PHP lately. I'm probably going to be posting a lot of question topics for a while :p