Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8310151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T19:21:23+00:00 2026-06-08T19:21:23+00:00

I am working on some images and want to assign different classes to them

  • 0

I am working on some images and want to assign different classes to them in batch.

here is my simple code

$image_dir = 'images';
$showimage = scandir($image_dir, 0);
unset($showimage [0],$showimage [1]);
print_r ($showimage );

the result is

Array ( 
    [2] => exmapleiamge1.png 
    [3] => exmapleiamge1_1.png 
    [4] => exmapleiamge1_2.png 
    [5] => exmapleiamge2_1.png 
    [6] => exmapleiamge2_2.png 
    [7] => exmapleiamge2_3.png 
    [8] => exmapleiamge3_1.png 
    [9] => exmapleiamge3_2.png 
)

What I want to achieve is that, assign div class=no1 to all images from exmapleiamge1_1 to exmapleiamge1_2, and assign class=no2 images from exmapleiamge2_1 to exmapleiamge2_3, and also for class no3…. etc;

I think the key is to filter the numbers, is it possible someone could give me a hand as I am a php beginner and keen to learn how to do that.

Many thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T19:21:25+00:00Added an answer on June 8, 2026 at 7:21 pm

    Here’s one way you can do it:

    foreach ($showimage as $image) {
        // Extract the number first
        $no = substr($image, strlen('exmapleiamge'), 1);
    
        $class = 'no'.$no;
    
        // Echo the HTML elements
        echo "<div class='$class'>";
        echo "<img src='$image'>";
        echo "</div>";
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on some code which fills a GridView full of images that have
There are some very useful classes for working with images. I am stuck with
I'm working on a project that contains some images in gallery, I want to
I'm working on a project and trying to render some images sitting under the
I have been working on a website that has some images that I need
am working with wallpaper app. In this i add some images in drawable folder
I have one AlertDialog which is working fine.I have set some background images to
I'm working on a menu (images made with indesign, not yet html): Some menu
ASP.NET Application I am having some confusion with showing images. IE is fully working
I am using frame animation for displaying some images. But it's working only in

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.