psquillace Posted October 30, 2007 Share Posted October 30, 2007 Hello All: I need some help with a script I have. I have this php script pasted below which grabs certain tags from a document and pastes it so you can print it out. What I need to do is have a form that people can fill in the address of the website they want to grab and have it return those results. I am not sure where to begin though being I am new to php. thanks for any help or advice, here is the script <?php $config['url'] = "http://www.yoursite.com"; // url of html to grab $config['start_tag'] = "<title>"; // where you want to start grabbing $config['end_tag'] = "</head>"; // where you want to stop grabbing $config['show_tags'] = 1; // do you want the tags to be shown when you show the html? 1 = yes, 0 = no class grabber { var $error = ''; var $html = ''; function grabhtml( $url, $start, $end ) { $file = file_get_contents( $url ); if( $file ) { if( preg_match_all( "#$start(.*?)$end#s", $file, $match ) ) { $this->html = $match; } else { $this->error = "Tags cannot be found."; } } else { $this->error = "Site cannot be found!"; } } function strip( $html, $show, $start, $end ) { if( !$show ) { $html = str_replace( $start, "", $html ); $html = str_replace( $end, "", $html ); return $html; } else { return $html; } } } $grab = new grabber; $grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] ); echo $grab->error; foreach( $grab->html[0] as $html ) { echo htmlspecialchars( $grab->strip( $html, $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/ Share on other sites More sharing options...
trq Posted October 30, 2007 Share Posted October 30, 2007 Remove this from the top.... $config['url'] = "http://www.yoursite.com"; // url of html to grab $config['start_tag'] = "<title>"; // where you want to start grabbing $config['end_tag'] = "</head>"; // where you want to stop grabbing $config['show_tags'] = 1; Now, down the bottom use.... <?php if (isset($_POST['submit'])) { $config['url'] = $_POST['url']; $config['start_tag'] = "<title>"; $config['end_tag'] = "</head>"; $config['show_tags'] = 1; $grab = new grabber; $grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] ); echo $grab->error; foreach( $grab->html[0] as $html ) { echo htmlspecialchars( $grab->strip( $html, $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>"; } } ?> All you need do now is point a form with a submit button named submit, and a text input named url toward this script and your done. Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381113 Share on other sites More sharing options...
psquillace Posted October 30, 2007 Author Share Posted October 30, 2007 Thanks Thorp for that help, One last thing though, I tried to make the echo part a bit prettier when it was echoed to the page but when I use \n anywhere in the echo part, it breaks the code. I also tried to use HTML inbetween the single quotes but that does not do it either. HOw can I get it to break where I want or make a table for it. Should I make a table then use includes? thanks again for your help, PM Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381125 Share on other sites More sharing options...
psquillace Posted October 30, 2007 Author Share Posted October 30, 2007 Ok, this might sound a bit stupid but, where do I point the action too. $config? I named the form fields url and submit but I am not sure of the form action... :-\ thanks Paul Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381128 Share on other sites More sharing options...
trq Posted October 30, 2007 Share Posted October 30, 2007 The form action should be the name of the file containing the above script. Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381130 Share on other sites More sharing options...
psquillace Posted October 30, 2007 Author Share Posted October 30, 2007 Oh, I was using <?php echo $_SERVER['PHP_SELF']; ?> so what you are saying is use just the absolute path then... Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381135 Share on other sites More sharing options...
psquillace Posted October 30, 2007 Author Share Posted October 30, 2007 Ok, so basically this is what I got but it is not working..... <?php if (isset($_POST['submit'])) { $config['url'] = $_POST['url']; $config['start_tag'] = "<title>"; $config['end_tag'] = "</head>"; $config['show_tags'] = 1; $grab = new grabber; $grab->grabhtml( $config['url'], $config['start_tag'], $config['end_tag'] ); echo $grab->error; foreach( $grab->html[0] as $html ) { echo htmlspecialchars( $grab->strip( $html, $config['show_tags'], $config['start_tag'], $config['end_tag'] ) ) . "<br>"; } } ?> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <title>Meta Tag Grabber</title> </head> <body> <form action="http://www.kardwelldev.com/meta-tag-grabber2.php" method="GET"> <input type="text" name="url"> <input type="submit" name="submit"> </form> </body> </html> sorry to be such a newb on this, PM Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381140 Share on other sites More sharing options...
psquillace Posted October 30, 2007 Author Share Posted October 30, 2007 Ok, Forget it, I got it. thanks for your help thorp. PM Quote Link to comment https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/#findComment-381358 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.