Loans - MPAA - Car Loan - Epson Stylus Photo Printer - Secured Loans

PDA

View Full Version : read a file and find phase between two tags


hasbehas
Jan 27th 2007, 2:48 am
My need is ;

I have htm files that have <h4>Title of Article</h4>
On most files this would be at top.

I need a function that will read $pname which is a htm file and find the phase between h4 tags and store it on a var $phase="Title of Article";

How this can be done ?

nico_swd
Jan 27th 2007, 3:47 am
preg_match('/<h4>([^<]+)<\/h4>/', file_get_contents($pname), $match);

$phase = $match[1];

echo $phase;



Untested.

chopsticks
Jan 27th 2007, 4:01 am
Missing a ) there. :P

Just thinking though, would what you just put above output "Array" if there were more than one match?

hmm....

nico_swd
Jan 27th 2007, 4:27 am
preg_match() stops searching after the first match. preg_match_all() would be needed to match more. But even if, I assigned $phase to $match[1], which is the second item in the array, and the first match from the regex. So no "Array" output there...

And where am I missing a bracket?

hasbehas
Jan 27th 2007, 5:00 am
Thanks.. It has done exactly what I have wanted except that "file_get_contents" only works on php5 and above. I had to test it on webserver rather localhost. But thanks..

nico_swd
Jan 27th 2007, 5:10 am
file_get_contents() is available since 4.3.0.

You can use this as alternative.


if (!function_exists('file_get_contents'))
{
function file_get_contents($filepath)
{
$handle = fopen($filepath, 'r');
$contents = fread($handle, filesize($filepath));
fclose($handle);

return $contents;
}
}

chopsticks
Jan 27th 2007, 5:18 am
preg_match() stops searching after the first match. preg_match_all() would be needed to match more. But even if, I assigned $phase to $match[1], which is the second item in the array, and the first match from the regex. So no "Array" output there...

And where am I missing a bracket?
Sorry, my eyes were playing up, I just looked again. Disregard that. *oops*

Thanks for the insight though about it only giving the first match. I get mixed up with them 2 sometimes.

pr0xy122
Jan 27th 2007, 5:43 am
i am with nico adjust with preg.

hasbehas
Jan 29th 2007, 4:25 am
thanks.. Very helpfull..