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; ?> 
  • If I understand your question correctly, you can try the following method. We take the name of the file and the name of each picture. Using the 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 the stripos function. It turns out that the name of the image contains the corresponding file name, and if a match is found, then continue. - tigrische
  • But what about ..._ n (..._ n.jpg)? - Sergey_777
  • The stripos 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

1 answer 1

 $images = array( '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' ); // имя Ρ„Π°ΠΉΠ»Π° PHP $php_filename = basename($_SERVER['PHP_SELF']); // ΠΎΠ±Ρ€Π΅ΠΆΠ΅ΠΌ `.php` $php_filename = substr($php_filename, 0, -4); // ΠΈΡΠΊΠ°Ρ‚ΡŒ Ρ‚Ρ€Π΅Ρ‚Π΅Π΅ Π²Ρ…ΠΎΠΆΠ΄Π΅Π½ΠΈΠ΅ Π½ΡƒΠΆΠ½ΠΎ Π½Π΅ ΠΏΠΎ ΠΈΠΌΠ΅Π½ΠΈ, Π° ΠΏΠΎ порядку $stop = 3; foreach ($images as &$image) { // ΠΈΡΠΊΠ°Ρ‚ΡŒ подстроку Π² строкС if (($srch = strripos($image, $php_filename)) !== FALSE) { echo "<img src='$image'>\n"; --$stop; } // Ссли Π½Π°ΠΉΠ΄Π΅Π½Π° Ρ‚Ρ€Π΅Ρ‚ΡŒΡ ΠΊΠ°Ρ€Ρ‚ΠΈΠ½ΠΊΠ°, Ρ‚ΠΎ стоп if (!$stop) { break; } } unset($image); 
  • Slightly changed the code to a more acceptable one. I can't integrate your code into a modified script. I'm confused about something. Please help! Does not work. Where can there be a mistake? - Sergey_777
  • Where does the execution fail? You already mastered the debugger? - toxxxa
  • So. Slightly corrected. Thank you, it works. Searches and displays 3 photos. If you can still tell. Constantly output happens: img_1.jpg, img_10.jpg, but should img_1.jpg, img_2.jpg ... Why is this happening? Where the script is - Modified ... - Sergey_777
  • one
    Used instead of sort function natcasesort - solution to this problem - Sergey_777
  • if everything worked out, correct the original question message. so that a visitor who comes by chance here can figure out what was discussed without reading the code sheet) - toxxxa