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 6147399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T19:06:16+00:00 2026-05-23T19:06:16+00:00

I’d like to add a regex filter to the script below (just before the

  • 0

I’d like to add a regex filter to the script below (just before the array_push) so that, when it encounters files that have the same prefix “_filename” and one of the matches has ‘-150×150.’ at the end of the filename, it pushes that image and skips the others with the same prefix.

However, if there are no images in that collection that have ‘-150×150.’ in the filename, it selects the image with the root prefix (_filename.ext)

For example, if the following files reside in the $dir folder:

_file.png
_file-150x150.png
_file-200x300.png
_someImage.png
_myfile.jpg
_myfile-200x200.jpg

I’d like it to push _file-150×150.png, someImage.png and myfile.jpg into the array, and skip over the others.

$dir = 'wp-content/uploads/';
if(!is_dir($dir)){return;}
$url = get_bloginfo('url').'/wp-content/uploads/';

$imgs = array();
if ($dh = opendir($dir)) 
{
    $myfilter="";
    while (($file = readdir($dh)) !== false) 
        {
        if (!is_dir($file)&& $file != "." && $file != ".." && preg_match("/^[".$myfilter."_].*\.(bmp|jpeg|gif|png|jpg)$/i", $file)) 
            {
                array_push($imgs, $file);
            }
        }
        closedir($dh);
    } else {
        die('cannot open ' . $dir);
    }
    if($imgs) 
    {
        sort($imgs); 
        echo '<div>';
        foreach ($imgs as $idx=>$img) 
        {
            $class = ($idx == count($imgs) - 1 ? ' class="last"' : '');
            echo $prelink.' src="' . $url . $img . '" alt="' .$img . '"' . $class . ' />'.$postlink;
        }
        echo '</div>';
    }
  • 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-05-23T19:06:17+00:00Added an answer on May 23, 2026 at 7:06 pm

    Assuming that the 150×150 filename will always come after the base class, here’s a regex you can use for that:

    /^([^-.]+)(?:-150x150)?\.png\b(?!.*\1-150x150\.png\b)/ms
    

    Explanation:

    /
    ^                        #Match the start of the line
    ([^-.]+)                 #Match the first part of the file before the -sizeXsize
                             #And store it in the first capturing group.
    (?:-150x150)?            #Match 0 or 1 of the desired size.
                             #(?:) is so that you don't store this match.
    \.png\b                  #followed by .png
    (?!.*\1-150x150\.png\b)  #A negative look ahead for the desired resolution.
                             #i.e. don't match the base if the desired resolution exists.
    /ms                      #m is for multiline so that the ^ means start of line rather than start of string.
                             #s is for dotall so that the .* in the lookahead can match newlines.
    

    The result will be something like this:

    Array
    (
        [0] => Array
            (
                [0] => _file-150x150.png
                [1] => _someImage.png
            )
    
        [1] => Array
            (
                [0] => _file
                [1] => _someImage
            )
    
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have just tried to save a simple *.rtf file with some websites and
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I would like to count the length of a string with PHP. The string
For some reason, after submitting a string like this Jack’s Spindle from a text

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.