Page 1 of 1

Pulling Contents from a file

Posted: Tue Jun 13, 2006 10:13 am
by spartan02
Ok, heres what I want to do, but have no idea if its possible or if there are any classes/functions available to do it.

Someone enters a word into a page, the code then checks another file for certain pre-defined items, pulling certain words/lines etc from it, then the code displays its findings back on the page.

bah.. its perfectly clear in my head how i want it to work but explaining it is a little tricky..

Posted: Tue Jun 13, 2006 10:24 am
by TheMoose
So you want to search a flat file for specific text in it?

Use file_get_contents() to return the file into a string, and using something like strpos() to find the location of the word in your file.

http://www.php.net/file_get_contents
http://www.php.net/strpos

Posted: Tue Jun 13, 2006 2:37 pm
by spartan02
Not a flat file no...

i want to do a dns lookup on a domain, like the following:

; <<>> DiG 9.2.1 <<>> devnetwork.net
;; global options: printcmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 12024
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 0, ADDITIONAL: 0

;; QUESTION SECTION:
;devnetwork.net. IN A

;; ANSWER SECTION:
devnetwork.net. 14400 IN A 63.247.70.254

;; Query time: 126 msec
;; SERVER: 192.168.2.1#53(192.168.2.1)
;; WHEN: Tue Jun 13 20:18:42 2006
;; MSG SIZE rcvd: 48


I just want to pull the following info from it:
devnetwork.net. 14400 IN A 63.247.70.254

or:
host -ptr devnetwork.net
host: illegal option -- p
Warning: invalid type: r
devnetwork.net has address 63.247.70.254

Now
devnetwork.net doesnt have a PTR record so after doing this check I want it to say "No PTR record found".

Posted: Tue Jun 13, 2006 5:23 pm
by derchris
Then I would use PHP functions instead of calling a system program.

Posted: Tue Jun 13, 2006 5:52 pm
by Christopher
Not exactly clear. It sounds like you want to do TWO things. First is do the DNS lookup and put the output into a string. Second it get a sub-string out of that output string.

Posted: Wed Jun 14, 2006 2:58 am
by spartan02
hehe knew it would be tricky to explain.

arborint that sounds about right.... i need to tweak the inforamtion taken from a lookup to take only the relevant stuff from it.. like the IP or MX records etc.

Posted: Wed Jun 14, 2006 3:02 am
by RobertGonzalez
Like arborint said, two things. Hit the DNS record, read it into a string. Then regex the string for what you need and return that to the script for cleaning and echoing.

Posted: Wed Jun 14, 2006 3:20 am
by spartan02
Everah thats spot on..

what functions should I be looking into to do this?

Posted: Wed Jun 14, 2006 10:00 am
by RobertGonzalez
Grabbing a DNS, I have no idea (but I am almost certain someone here does). As for all of the other stuff you can have a look at regular expressions and file system functions. File system will be a lot easier to wrap your mind around than regular expressions (unless you have some experience with regex already).

Posted: Wed Jun 14, 2006 10:11 am
by spartan02
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


I havent been able to get my head fully around regexpression... here is something thats ive patched together to give you an idea:

Code: Select all

<?php
$output = `/usr/bin/host -t ns $query`;

$find [] = "Host $query not found: 3(NXDOMAIN)";
$find [] = "$query name server";
$replace [] = " Domain Not Found - Please make sure you've spelt it correctly";
$replace [] = "";

$output =  str_replace($find, $replace, $output);

if (preg_match("^[a-z0-9\:\.\-]+$^", "$output $query"))
{
                echo "Name Servers:<pre>$output</pre>";
}
elseif ($domain == ""){
                echo "";
}
else {
                echo "You have entered an invalid domain name, do not enter 'http://' or 'www.'";
}
?>

As you can see I am using the Find/Replace function to remove what I dont want to be shown.

This seems like the long way around, and i could use the find/repalce fucntion to remove everything I dont want and leave the stuff I do..

I am using this for my new website http://www.domaindiagnosis.com


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]