pbellem Posted August 27, 2009 Share Posted August 27, 2009 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 More sharing options...
Mark Baker Posted August 27, 2009 Share Posted August 27, 2009 Try the ungreedy flag preg_match("/<h2.*>(.*)<\/h2>/Us",$pdfer[2],$myTitle); Link to comment https://forums.phpfreaks.com/topic/172122-solved-help-with-preg_match/#findComment-907544 Share on other sites More sharing options...
pbellem Posted August 27, 2009 Author Share Posted August 27, 2009 Thanks that worked. Dumb flags!! Link to comment https://forums.phpfreaks.com/topic/172122-solved-help-with-preg_match/#findComment-907555 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.