Jump to content

[SOLVED] Help with Preg_match


pbellem

Recommended Posts

Hello,

 

I am having some issues with preg_match. From my understanding it is supposed to return the first occurrence of a match.  However I am running into issues where it is returning the second occurrence.

 

I read files from a directory.

 

if ($handle = @opendir('' .$directory)) {

 

  while (false !== ($file = readdir($handle))) {

   

    if($file !== "." and $file !== ".." and $file != "test.php") {

$ext = substr($file, -6);

if (strcasecmp($ext,"_".$language.".php")==0)

{     

$fd = fopen($directory.$file, "r");

        $content = fread($fd, filesize($directory.$file));

fclose($fd);

        $phpFiles[] = array(filemtime($directory.$file), $file, $content); 

}

else

{

            //do nothing

}

      }

     

  }

  closedir($handle);

  rsort($phpFiles);

 

I then read through the files looking for <h2 class="something">Text</h2>

 

I am using

 

foreach ($phpFiles as $pdfer){

 

preg_match("/<h2.*>(.*)<\/h2>/s",$pdfer[2],$myTitle);

 

echo '<title>' . $myTitle[1] . '</title>';

 

This works, but on files that have two <h2> tags it always goes to the second <h2>

 

ie

 

<h2>My title</h2>

 

<h2>something else</h2>

 

It always prints out "something else" as my title.

Thanks,

Paulo

Link to comment
https://forums.phpfreaks.com/topic/172122-solved-help-with-preg_match/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.