Current location: Hot Scripts Forums » Programming Languages » PHP » Help with Regex in PHP


Help with Regex in PHP

Reply
  #1 (permalink)  
Old 06-05-03, 09:48 AM
HS Staff's Avatar
HS Staff HS Staff is offline
HotScripts.com Forum Admin
 
Join Date: May 2003
Location: 2
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Help with Regex in PHP

Hello All:

I am needing some PHP code that will run a regex on a string (the environmental variable for a users hostname) and return the fully qualified domain name from that string. Below are some examples of strings:

poolf7-029.wwa.com
whatever.sub.sub2.domain.co.uk
morestuff.more-stuff.sub.school.k12.us

You get the idea. If we were looking at just a single .xxx it would be simple, but .com.ty or .co.uk and perhaps even 3-level extensions make this more challenging.

Anyone know of some code that has been written to parse fully qualified domain names out of hostnames?
Reply With Quote
  #2 (permalink)  
Old 06-05-03, 11:01 AM
Pearl's Avatar
Pearl Pearl is offline
Newbie Coder
 
Join Date: Jun 2003
Location: Canada
Posts: 54
Thanks: 0
Thanked 0 Times in 0 Posts
Reply With Quote
  #3 (permalink)  
Old 06-06-03, 02:10 AM
phpkid phpkid is offline
Hot Moderator ;)
 
Join Date: Jun 2003
Location: India
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
That is an interesting problem.
Here is what I could come up with.

PHP Code:

<?php


//You will have to keep list of Fully qualified domain nam in $domain_names array.
$domain_names[] = '.abc.dk';
$domain_names[] = '.com';
$domain_names[] = '.hi';
$domain_names[] = '.hello';
$domain_names[] = '.mark';
$domain_names[] = '.in';

//Supply domain name to check in $domain variable.
$domain 'hello.com.in';

$num_levels 3//Number of levels (or extensions) to go to look in a domain!


//Start Processing...

//Find number of dots.
$num_dots substr_count($domain,'.');

$domain_dup $domain//Copy the original domain name.
if($num_dots == 0  )
{
    print 
'Not a valid domain!'
}
else
{
    
$level 0//We will start from last DOT till the number of levels we are looking for.
    
while($level $num_levels)
    {
        
$domain_part '.' substr(strrchr($domain_dup,'.'),1);
        
$domain_root[] = $domain_part $domain_root[count($domain_root)-1]; 
        
//$domain_dup = str_replace($domain_root[count($domain_root)-1],'',$domain_dup);
        
$domain_dup str_replace($domain_part,'',$domain_dup);

        
$level++;        
        if(
$level == $num_dots)
            break;
        
    }
    
//nl2br(print_r($domain_root));
}

//As we have build up list of fully qualified domain names, now search throuh our existing list!
foreach($domain_root as $domain_to_check)
{
    if(
in_array($domain_to_check,$domain_names))
    {
        
$found TRUE;
        print 
"Fully qualified domain name for <h1>$domain</h1> is <h2>$domain_to_check</h2>";    
    }

}
if(! 
$found)
{
    print 
'Invalid domain name.';
}
?>
Try it and see if it works. I think it might not work in some situations and I am right now too lazy to think about those situations.

So if you could populate $domain_names properly and check it, it will be great!

Regards,
JD
__________________
http://www.phpkid.org
Reply With Quote
  #4 (permalink)  
Old 06-06-03, 02:19 AM
phpkid phpkid is offline
Hot Moderator ;)
 
Join Date: Jun 2003
Location: India
Posts: 69
Thanks: 0
Thanked 0 Times in 0 Posts
Okie,
I figured out when my code will break.
Say if you have top qualified domain name like .co.in and .in then it will tell you that top qualified domain name is .in instead of correct '.co.in' . Anyways following code solves the problem!

PHP Code:

<?php


$domain 
'india.co.in';

print 
"Fully qualified name for the domain $domain is " '<h1>' fully_qualified_name($domain) . '</h1>';

?>
<?php

function fully_qualified_name($domain)
{
    
//You will have to keep list of Fully qualified domain nam in $domain_names array.
    
$domain_names[] = '.abc.dk';
    
$domain_names[] = '.com';
    
$domain_names[] = '.hi';
    
$domain_names[] = '.hello';
    
$domain_names[] = '.mark';
    
$domain_names[] = '.in';
    
$domain_names[] = '.co.in';


    
$num_levels 3//Number of levels (or extensions) to go to look in a domain!


    //Start Processing...

    //Find number of dots.
    
$num_dots substr_count($domain,'.');

    
$domain_dup $domain//Copy the original domain name.
    
if($num_dots == 0  )
    {
        return 
'Not a valid domain!'
    }
    else
    {
        
$level 0//We will start from last DOT till the number of levels we are looking for.
        
while($level $num_levels)
        {
            
$domain_part '.' substr(strrchr($domain_dup,'.'),1);
            
$domain_root[] = $domain_part $domain_root[count($domain_root)-1]; 
            
//$domain_dup = str_replace($domain_root[count($domain_root)-1],'',$domain_dup);
            
$domain_dup str_replace($domain_part,'',$domain_dup);

            
$level++;        
            if(
$level == $num_dots)
                break;
            
        }
        
//nl2br(print_r($domain_root));
    
}

    
$domain_root array_reverse($domain_root);

    
//As we have build up list of fully qualified domain names, now search throuh our existing list!
    
foreach($domain_root as $domain_to_check)
    {
        if(
in_array($domain_to_check,$domain_names))
        {
            return 
$domain_to_check;
        }

    }
    return 
'Invalid domain name.';

}

?>
Hope that helps!
JD
__________________
http://www.phpkid.org

Last edited by phpkid; 06-06-03 at 02:22 AM.
Reply With Quote
  #5 (permalink)  
Old 06-06-03, 05:51 PM
DuEy's Avatar
DuEy DuEy is offline
PHP is my life.
 
Join Date: Jun 2003
Location: New Zealand
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
This returns the domains that are real you can modify it to only work on the first domain.
PHP Code:

<?

$domain 
"php.is.the.best.netdupe.com";

$darray explode("."$domain);
//some calculations
$total count($darray);
$sub $total -1;
$buffer $darray[$total];

for(
$i=$sub;$i>=0$i--){
$buffer $darray[$i].".".$buffer;
//more calculations
$buffer trim(str_replace("."" "$buffer));
$buffer str_replace(" ""."$buffer);
//$buffer = $buffer{$length};
$ip gethostbyname($buffer);

if(
ereg('^([0-9]+\.)+([0-9]{1,3})+(\.)+([0-9]{1,3})+(\.)+[0-9]{1,3}$',$ip)){
echo 
$buffer."<br>";
}
}
?>
this echo's "netdupe.com" as it is the only real domain.

if you want it to only return the top level domain (no subdomains) you will need to make all the real domains into a string seperated by a comma. should look something like this:

PHP Code:

if(ereg('^([0-9]+\.)+([0-9]{1,3})+(\.)+([0-9]{1,3})+(\.)+[0-9]{1,3}$',$ip)){

$end .=  $buffer.",";
}
}
$buffer explode(","$end);
echo 
$buffer[0];
?> 
go here for the proper file the forums strip stuff that needs it to work.
http://www.devdino.com/domaincheck.txt
__________________
-Duey
server: irc.chatchannel.org
channel: #hotscripts

Last edited by DuEy; 06-06-03 at 07:35 PM.
Reply With Quote
  #6 (permalink)  
Old 06-07-03, 01:32 AM
DuEy's Avatar
DuEy DuEy is offline
PHP is my life.
 
Join Date: Jun 2003
Location: New Zealand
Posts: 44
Thanks: 0
Thanked 0 Times in 0 Posts
Ok ive modified it a bit to try and speed the script up, tested and works fine. I enjoyed making this one lol. See attached file.
Attached Files
File Type: txt domaincheck.txt (1.0 KB, 482 views)
__________________
-Duey
server: irc.chatchannel.org
channel: #hotscripts

Last edited by DuEy; 06-07-03 at 01:42 AM.
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
PHP and MySQL ? rob2132 Hot Scripts Forum Questions, Suggestions and Feedback 4 08-29-08 02:22 AM
PHP Dudes - Hi All stuart New Members & Introductions 4 05-03-04 01:22 PM
Custom PHP Scripts BdSBB Job Offers & Assistance 3 12-07-03 09:49 AM
PHP Triad/Upload form eddyvlad PHP 6 10-06-03 11:17 PM
PHP with JAVA funkydunk PHP 0 07-21-03 09:40 AM


All times are GMT -5. The time now is 07:50 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.