Replace in one step based on matched result

Craig

I have a string and I want each YYYY-MM-DD HH:MM:SS date time string to be replaced with a unix timestamp.

I have managed to get as far as identifying where date time strings occur:

$my_string = 'Hello world 2014-12-25 10:00:00 and foo 2014-09-10 05:00:00, bar';

preg_match_all('((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))',$my_string,$my_matches, PREG_OFFSET_CAPTURE);

print_r($my_matches);

This outputs an array of arrays containing the value of the date time string that was matched and its location:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => 2014-12-25 10:00:00
                    [1] => 12
                )

            [1] => Array
                (
                    [0] => 2014-09-10 05:00:00
                    [1] => 40
                )

        )

)

From this point I was going to loop through the arrays and replace based on str_replace() and strtotime() however I'm thinking it would have lower execution time if I could do something like this:

$my_string = preg_replace(
    '((?:2|1)\\d{3}(?:-|\\/)(?:(?:0[1-9])|(?:1[0-2]))(?:-|\\/)(?:(?:0[1-9])|(?:[1-2][0-9])|(?:3[0-1]))(?:T|\\s)(?:(?:[0-1][0-9])|(?:2[0-3])):(?:[0-5][0-9]):(?:[0-5][0-9]))',
    strtotime($VALUE_OF_MATCHED_STRING),
    $my_string
);

So that every found instance of the matched would simply be made to strtotime() format.

What is the correct way to get this result? Is looping the most feasible way?

Amal Murali

Use preg_replace_callback() instead. It allows you to perform a search and replace using a callback function:

echo preg_replace_callback($pattern, function ($m) {
    return strtotime($m[0]);
}, $my_string);

$m is the array containing the matched items. $m[0] contains the date string.

The above code would output:

Hello world 1419501600 and foo 1410325200, bar

Demo

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Replace matched patterns in a string based on condition

分類Dev

How to show up matched result when input text is delete one by one

分類Dev

.replace with matched pattern +1 (integer)

分類Dev

Replace Index values with matched values in other dataframe

分類Dev

How to replace a matched group value with Regex

分類Dev

Fetch all rows until a special one matched

分類Dev

Forward declaring and using in one step

分類Dev

copy and sort an NSArray in one step

分類Dev

Regex C# replace matched fields with multiple results

分類Dev

one to one result in mysql Query

分類Dev

Using select result to replace string

分類Dev

Vuejs dynamic step set based on ranger value

分類Dev

Jquery ui slider: Set step based on checkbox

分類Dev

remove all results of join if one record of join is not matched

分類Dev

preg_replace for template calling function that provides data to replace matched expression

分類Dev

Mutate/replace in one go

分類Dev

How to return hash and bytes in one step in Go?

分類Dev

iteratively adding elements to list in one step

分類Dev

Tar and download in one step over ssh / scp

分類Dev

How to cut a Video and mute it in one step with FFmpeg?

分類Dev

Iterate a Flux, execute a Mono inside, use the result in the next step

分類Dev

Replace values based on index pandas

分類Dev

What is the most minimalistic way for CLI search and replace using part of matched strings?

分類Dev

Replace two consecutive CR with one

分類Dev

How to change permissions for a folder and its subfolders/files in one step?

分類Dev

Pass output of "whereis" command to "cd" to change directory in one step

分類Dev

Scala future return based on first future result

分類Dev

Filter SQL query based on scalar function result

分類Dev

Update an existing table based on result of executing a variable

Related 関連記事

  1. 1

    Replace matched patterns in a string based on condition

  2. 2

    How to show up matched result when input text is delete one by one

  3. 3

    .replace with matched pattern +1 (integer)

  4. 4

    Replace Index values with matched values in other dataframe

  5. 5

    How to replace a matched group value with Regex

  6. 6

    Fetch all rows until a special one matched

  7. 7

    Forward declaring and using in one step

  8. 8

    copy and sort an NSArray in one step

  9. 9

    Regex C# replace matched fields with multiple results

  10. 10

    one to one result in mysql Query

  11. 11

    Using select result to replace string

  12. 12

    Vuejs dynamic step set based on ranger value

  13. 13

    Jquery ui slider: Set step based on checkbox

  14. 14

    remove all results of join if one record of join is not matched

  15. 15

    preg_replace for template calling function that provides data to replace matched expression

  16. 16

    Mutate/replace in one go

  17. 17

    How to return hash and bytes in one step in Go?

  18. 18

    iteratively adding elements to list in one step

  19. 19

    Tar and download in one step over ssh / scp

  20. 20

    How to cut a Video and mute it in one step with FFmpeg?

  21. 21

    Iterate a Flux, execute a Mono inside, use the result in the next step

  22. 22

    Replace values based on index pandas

  23. 23

    What is the most minimalistic way for CLI search and replace using part of matched strings?

  24. 24

    Replace two consecutive CR with one

  25. 25

    How to change permissions for a folder and its subfolders/files in one step?

  26. 26

    Pass output of "whereis" command to "cd" to change directory in one step

  27. 27

    Scala future return based on first future result

  28. 28

    Filter SQL query based on scalar function result

  29. 29

    Update an existing table based on result of executing a variable

ホットタグ

アーカイブ