My need is ; I have htm files that have <h4>Title of Article</h4> Code (markup): 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 ?
preg_match('/<h4>([^<]+)<\/h4>/', file_get_contents($pname), $match); $phase = $match[1]; echo $phase; PHP: Untested.
Missing a ) there. Just thinking though, would what you just put above output "Array" if there were more than one match? hmm....
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?
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..
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; } } PHP:
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.