...

View Full Version : Help with a parser script please?



stfc_boy
12-15-2010, 10:40 AM
Hello,

I'm in the middle of writing a parser to ditch all the junk at the start of a file I wish to read, then when it finds a certain tag <HEAD> I want the details outputted to the page - ignoring everything before that tag, but i'm having problems and nothing is outputted.

Anyone kindly advise, think i'm nearly there?

Thanks



<?php

//* Open the file
$lines = file('file.html');

//* Need to ditch the junk at the start of the file so initially set $find_line = false
$find_line = false;

//* Loop through the lines
foreach ( $lines as $line ) {

//* Check find the line where the <HEAD> tag is and then set $find_line = true
if(strpos($line, '<HEAD>')){
$find_line = true;
}

//* Now it's 'true' echo out details ignorning everything before <head>
if($find_line){
echo $val = trim ($line);
}

} //End foreach

?>


Thanks

Chris

mlseim
12-15-2010, 12:29 PM
Not sure if this helps or not .... I found this using Google:

For getting the <title>, but I suppose it could just as well be <head> ...

$data = file_get_contents("SOME URL");
$pattern = "/<title>([^<]*)<\/title>/";
preg_match($pattern, $data, $matches);
//$matches is an array;
$matches[1] will get the the data in the first () or the title $title = $matches[1];

MattF
12-15-2010, 06:39 PM
Using that concept mlseim posted:



$data = file_get_contents("SOME URL");
$pattern = "~<head>(.*)~is";
preg_match($pattern, $data, $matches);
print($matches[1]);


Use $matches[0] instead of 1 if you want to print the head tag too.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum