read a file and find phase between two tags

Discussion in 'PHP' started by hasbehas, Jan 27, 2007.

  1. #1
    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 ?
     
    hasbehas, Jan 27, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    
    preg_match('/<h4>([^<]+)<\/h4>/', file_get_contents($pname), $match);
    
    $phase = $match[1];
    
    echo $phase;
    
    
    PHP:
    Untested.
     
    nico_swd, Jan 27, 2007 IP
  3. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #3
    Missing a ) there. :p

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

    hmm....
     
    chopsticks, Jan 27, 2007 IP
  4. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #4
    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?
     
    nico_swd, Jan 27, 2007 IP
  5. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #5
    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..
     
    hasbehas, Jan 27, 2007 IP
  6. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #6
    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:
     
    nico_swd, Jan 27, 2007 IP
  7. chopsticks

    chopsticks Active Member

    Messages:
    565
    Likes Received:
    20
    Best Answers:
    0
    Trophy Points:
    60
    #7
    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.
     
    chopsticks, Jan 27, 2007 IP
  8. pr0xy122

    pr0xy122 Peon

    Messages:
    1,649
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #8
    i am with nico adjust with preg.
     
    pr0xy122, Jan 27, 2007 IP
  9. hasbehas

    hasbehas Well-Known Member

    Messages:
    726
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    190
    #9
    thanks.. Very helpfull..
     
    hasbehas, Jan 29, 2007 IP