Page 1 of 1

PHP trimming page contents

Posted: Wed Sep 06, 2006 6:18 am
by Black_cage85
onion2k | 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'm creating a serach function and have got it to find a particular term in a document or web page. I'm now trying to get it to display the term found and the text either side of it but all I can get it to do is output the whole page contents. The page contents get stored in the variable $contents. I was wondering if anybody could help me, the code i'm using is displayed below, it is linked to from a page which just displays the results in a table.

Code: Select all

<?php
/*
 * Complete the search class below
 */
class SiteSearch {

function return_result () {
		
	$filename = $_GET['url'];

	$h=date("H",$t)-4;

	$date = date("l dS \of F Y $h:i:s A");
	$term = $_GET['term'];
	$depth = $_GET['depth'];

	$result = array();

	$handle = fopen($filename, "rb");
	$preg = "/$term/";
	preg_match_all(trim($preg),	file_get_contents($filename), $out, PREG_PATTERN_ORDER);
	$size = sizeof($out[0]);
	$i=0;

	while ($i < $size) {
		$title = $out[0][$i];
		$title = trim(strip_tags($title));
		$contents = file_get_contents($filename);

		$result[$i] = array (		
			"title"		=> ($i+1).": ". $title,
			"date"		=> $date,
			"content"	=> $contents,
			"url"		=> $filename
		  );
		  $i++;
		}
	return $result;
	fclose($handle);
	}
}
?>
Any help would be greatly appreciated

onion2k | 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]

Re: PHP trimming page contents

Posted: Wed Sep 06, 2006 6:57 am
by jmut
Could you tell us some sample content, and sample string/pattern you want to search for.

Posted: Wed Sep 06, 2006 7:03 am
by Black_cage85
I have it running at:

http://www.hilditchmedia.rr4.co.uk/task ... tm&depth=3

Where it has the results at the bottom it shows all the page contents but I would like it to display just the text around where it found the term, in this case Paul.

Posted: Wed Sep 06, 2006 3:43 pm
by feyd
strpos() + substr()

some math will be required.

Posted: Wed Sep 06, 2006 4:05 pm
by Mordred
1. You call file_get_contents() too much.
2. You have a vulnerability when $filename is a local file (luckily you don't print ['content'] on your result page, hehe ;) )
3. You have a possible CPU hog with using user-input for pattern matching
4. You would also match html markup which is probably not your intention.
5. This is generally not a good way to make text search, better use a "manual" reverse index, or mysql with FULLTEXT index

Edit:
6.

Code: Select all

$handle = fopen($filename, "rb"); //remove that and the fclose