Pulling Contents from a file

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
spartan02
Forum Newbie
Posts: 5
Joined: Tue Jun 13, 2006 10:06 am

Pulling Contents from a file

Post 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..
User avatar
TheMoose
Forum Contributor
Posts: 351
Joined: Tue May 23, 2006 10:42 am

Post 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
spartan02
Forum Newbie
Posts: 5
Joined: Tue Jun 13, 2006 10:06 am

Post 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".
derchris
Forum Commoner
Posts: 44
Joined: Sat Jun 10, 2006 6:14 pm

Post by derchris »

Then I would use PHP functions instead of calling a system program.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
spartan02
Forum Newbie
Posts: 5
Joined: Tue Jun 13, 2006 10:06 am

Post 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.
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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.
spartan02
Forum Newbie
Posts: 5
Joined: Tue Jun 13, 2006 10:06 am

Post by spartan02 »

Everah thats spot on..

what functions should I be looking into to do this?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post 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).
spartan02
Forum Newbie
Posts: 5
Joined: Tue Jun 13, 2006 10:06 am

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