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

  • Home
  • SEARCH
  • 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 4033556
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T11:51:40+00:00 2026-05-20T11:51:40+00:00

Hacking up what I thought was the second simplest type of regex (extract a

  • 0

Hacking up what I thought was the second simplest type of regex (extract a matching string from some strings, and use it) in php, but regex grouping seems to be tripping me up.

Objective

  1. take a ls of files, output the commands to format/copy the files to have the correct naming format.
  2. Resize copies of the files to create thumbnails. (not even dealing with that step yet)

Failure

My code fails at the regex step, because although I just want to filter out everything except a single regex group, when I get the results, it’s always returning the group that I want -and- the group before it, even though I in no way requested the first backtrace group.

Here is a fully functioning, runnable version of the code on the online ide:
http://ideone.com/2RiqN

And here is the code (with a cut down initial dataset, although I don’t expect that to matter at all):

<?php

// Long list of image names.
$file_data = <<<HEREDOC
07184_A.jpg
Adrian-Chelsea-C08752_A.jpg
Air-Adams-Cap-Toe-Oxford-C09167_A.jpg
Air-Adams-Split-Toe-Oxford-C09161_A.jpg
Air-Adams-Venetian-C09165_A.jpg
Air-Aiden-Casual-Camp-Moc-C09347_A.jpg
C05820_A.jpg
C06588_A.jpg
Air-Aiden-Classic-Bit-C09007_A.jpg
Work-Moc-Toe-Boot-C09095_A.jpg
HEREDOC;

if($file_data){
    $files = preg_split("/[\s,]+/", $file_data);
    // Split up the files based on the newlines.
}
$rename_candidates = array();
$i = 0;
foreach($files as $file){
    $string = $file;
    $pattern = '#(\w)(\d+)_A\.jpg$#i';
    // Use the second regex group for the results.
    $replacement = '$2';
    // This should return only group 2 (any number of digits), but instead group 1 is somehow always in there.
    $new_file_part = preg_replace($pattern, $replacement, $string);
// Example good end result: <img src="images/ch/ch-07184fs.jpg" width="350" border="0">
    // Save the rename results for further processing later.
    $rename_candidates[$i]=array('file'=>$file, 'new_file'=>$new_file_part);
    // Rename the images into a standard format.
    echo "cp ".$file." ./ch/ch-".$new_file_part."fs.jpg;";
        // Echo out some commands for later.
    echo "<br>"; 
    $i++;
    if($i>10){break;} // Just deal with the first 10 for now.
}
?>

Intended result for the regex: 788750
Intended result for the code output (multiple lines of): cp air-something-something-C485850_A.jpg ./ch/ch-485850.jpg;

What’s wrong with my regex? Suggestions for simpler matching code would be appreciated as well.

  • 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-20T11:51:41+00:00Added an answer on May 20, 2026 at 11:51 am

    Scan Dir and Expode

    You know what? A simpler way to do it in php is to use scandir and explode combo

      $dir = scandir('/path/to/directory');
        foreach($dir as $file)
    {
        $ext = pathinfo($file,PATHINFO_EXTENSION);
        if($ext!='jpg') continue;
    
        $a = explode('-',$file); //grab the end of the string after the -
        $newfilename = end($a); //if there is no dash just take the whole string
    
        $newlocation = './ch/ch-'.str_replace(array('C','_A'),'', basename($newfilename,'.jpg')).'fs.jpg';
        echo "@copy($file, $newlocation)\n";
    
    }
    #and you are done :)
    

    explode: basically a filename like blah-2.jpg is turned into a an array('blah','2.jpg); and then taking the end() of that gets the last element. It’s the same almost as array_pop();

    Working Example

    Here’s my ideaone code http://ideone.com/gLSxA

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm hacking around in a Firefox extension, trying to use E4X. However, I've come
i'd like to prevent bots from hacking weak password-protected accounts. (e.g. this happend to
I am asking this question from an educational/hacking point of view, (I wouldn't really
I come from a computer science. background, but I am now doing genomics. My
I'm hacking a quick and dirty python script to generate some reports as static
I am hacking up a tagging application for emacs. I have got a tag
I'm hacking on a simple Cocoa app to make blocks move around the screen
I was hacking away the source code for plink to make it compatible with
I know I can do most of this by hacking Trac and using Git
I have an OpenGL ES game that I am hacking together. One part of

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.