Jump to content

Recommended Posts

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>";
}

?>

Link to comment
https://forums.phpfreaks.com/topic/75356-solved-help-with-a-grabbing-script/
Share on other sites

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.

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

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.