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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T18:58:44+00:00 2026-06-04T18:58:44+00:00

I have a large text containing many dates formatted like this: 10 april 2012,

  • 0

I have a large text containing many dates formatted like this:

 10 april 2012, monday

I need to transform all of them into this format:

 \r\nmonday, 10 april 2012

So, I wrote a regex and it works perfectly OK.

 $matches1= preg_replace(
 '#(\d{,2} [a-z]+) \d{4}, (sunday|monday|tuesday|wednesday|thursday|friday|saturday)#u', 
 "\r\n$2$3$4$5$6$7$8, $1", 
 $txt);

The problem is that I also need to save all the transformed parts of the text matching the replacement pattern – “\r\n$2$3$4$5$6$7$8, $1” (like \r\nmonday, 10 april 2012) – into an array. So that I have something like this:

 Array('\r\nmonday, 10 april 2012', '\r\ntuesday, 11 april 2012', '\r\nfriday, 14 april 2012' etc.)

Is that possible?

Replacement pattern (“\r\n$2$3$4$5$6$7$8, $1”) comes from a html form and may vary.

Update

I’ve tried to write a callback function but I couldn’t get the result I needed.
So I’ve come up with the following:

 $text = ...;//some text
 $search = ...;//search pattern
 $replacement = ...;//replacement pattern

 preg_match_all('#' . $search. '#u', $text, $matches, PREG_SET_ORDER);

 foreach ($matches as $match) 
 {
     $replacements[] = preg_replace('#' . $search. '#u', $replacement, $match[0]);
 }

 $newtext = preg_replace('#' . $search. '#u', $replacement, $text);

So $newtext contains the transformed text and $replacenemts contains all the replacements.

  • 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-04T18:58:45+00:00Added an answer on June 4, 2026 at 6:58 pm

    Use preg_replace_callback instead (or exemplary in the following code-example additionally) and keep track of all replacements:

    $search =  '#(\d{,2} [a-z]+) \d{4}, (sunday|monday|tuesday|wednesday|thursday|friday|saturday)#u';
    $replace = "\r\n$2$3$4$5$6$7$8, $1";
    $captured = array();
    
    preg_replace_callback($search, function($matches) use (&$captured)  {
        $captured[] = $matches;    
    }, $txt);
    
    $matches1= preg_replace($search, $replace, $txt);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have memory mapped a large formatted (text) file containing one integer per line
I have a large text file I am reading from and I need to
I have a a large text file (over 70mb) and need to count the
I have a large text resource in my bundle. It's a CSV containing lines
On a Linux system, I have a very large text file and I need
I have a large (~50Mb) file containing poorly formatted XML describing documents and properties
I have a tab-delimited text file that is very large. Many lines in the
I have a large number of text files (1000+) each containing an article from
I have a large text file. Each time my program runs, it needs to
I have several large text files (30m+ lines, >1GB) which are being processed 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.