There is a folder with a photo ./img/mega_foto_2/
This folder contains images:
acura_ilx_endurance_racer_2013_1.jpg acura_ilx_endurance_racer_2013_2.jpg acura_ilx_endurance_racer_2013_3.jpg acura_ilx_endurance_racer_2013_4.jpg ....... acura_nsx_2016_1.jpg acura_nsx_2016_2.jpg acura_nsx_2016_3.jpg acura_nsx_2016_4.jpg alfa_romeo_giulia_2016_1.jpg alfa_romeo_giulia_2016_2.jpg alfa_romeo_giulia_2016_3.jpg alfa_romeo_giulia_2016_4.jpg alfa_romeo_pandion_concept_2010_1.jpg alfa_romeo_pandion_concept_2010_2.jpg alfa_romeo_pandion_concept_2010_3.jpg alfa_romeo_pandion_concept_2010_4.jpg
For example, the file is called acura_ilx_endurance_racer_2013.php
How to make a conclusion on the file name? For example, if the file is acura_ilx_endurance_racer_2013.php, then output only a photo with the name acura_ilx_endurance_racer_2013_1.jpg, acura_ilx_endurance_racer_2013_2.jpg ..... acura_ilx_endurance_racer_2013_n.jpg
<?php $directory = './img/mega_foto_2'; //Π½Π°Π·Π²Π°Π½ΠΈΠ΅ ΠΏΠ°ΠΏΠΊΠΈ Ρ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΡΠΌΠΈ $allowed_types=array('jpg','jpeg'); //ΡΠ°Π·ΡΠ΅ΡΠ΅Π½ΡΠ΅ ΡΠΈΠΏΡ ΠΈΠ·ΠΎΠ±ΡΠ°ΠΆΠ΅Π½ΠΈΠΉ $file_parts=array(); $ext=''; $title=''; $i=0; //ΠΏΡΠΎΠ±ΡΠ΅ΠΌ ΠΎΡΠΊΡΡΡΡ ΠΏΠ°ΠΏΠΊΡ $dir_handle = @opendir($directory) or die("There is an error with your image directory!"); // ΠΈΠΌΡ ΡΠ°ΠΉΠ»Π° PHP $php_filename = basename($_SERVER['PHP_SELF']); // ΠΎΠ±ΡΠ΅ΠΆΠ΅ΠΌ `.php` $php_filename = substr($php_filename, 0, -4); $counter = 0; //Π·Π°Π²ΠΎΠ΄ΠΈΠΌ ΡΡΠ΅ΡΡΠΈΠΊ for($i=0; $i<=(sizeof($php_filename)-1); $i++) { $files=array(); while ($file = readdir($dir_handle)) //ΠΏΠΎΠΈΡΠΊ ΠΏΠΎ ΡΠ°ΠΉΠ»Π°ΠΌ { if($file=='.' || $file == '..') continue; array_push($files,$file); } sort($files); $stop = 3; foreach ($files as $file) { if (($srch = strripos($file, $php_filename)) !== FALSE) { echo '<div id="container"><img src="js/blank.gif" data-src="'.$directory.'/'.$file.'" /></div>'; --$stop; } $counter++; //ΡΠ²Π΅Π»ΠΈΡΠΈΠ²Π°Π΅ΠΌ ΡΡΠ΅ΡΡΠΈΠΊ Π½Π° Π΅Π΄ΠΈΠ½ΠΈΡΠΊΡ } // Π΅ΡΠ»ΠΈ Π½Π°ΠΉΠ΄Π΅Π½Π° ΡΡΠ΅ΡΡΡ ΠΊΠ°ΡΡΠΈΠ½ΠΊΠ°, ΡΠΎ ΡΡΠΎΠΏ if (!$stop) { break; } $i++; } closedir($dir_handle); //Π·Π°ΠΊΡΡΡΡ ΠΏΠ°ΠΏΠΊΡ ?> <?php echo $counter; ?>
explode
function,explode
break them into two parts: to the point and after the point. The part that is after the point is discarded, and the first part is written to the variable separately for the file and the picture. Then compare the occurrence of the file name in the picture name using thestripos
function. It turns out that the name of the image contains the corresponding file name, and if a match is found, then continue. - tigrischestripos
function returns the position of the first occurrence of a substring case-insensitive. That is, if we compare the lines acura_ilx_endurance_racer_2013 and acura_ilx_endurance_racer_2013_1, then it will find the first line in the second and return 0 in this case, which means - a match is found from position 0. - tigriste