The correct regular expression date pattern for dd / mm / yyyy

I need to update the same line that also includes the date in the format dd/mm/yyyyalong with some line in the file group. I checked the answers here to similar questions, but could not make any of the proposed templates in my code.

My current PHP code is:

<?php
// get the system date
$sysdate = date("d/m/Y");
// open the directory
$dir = opendir($argv[1]);
$files = array();
// sorts the files alphabetically
while (($file = readdir($dir)) !== false) {
    $files[] = $file;
}
closedir($dir);
sort($files);
// for each ordered file will run the in the clauses part
foreach ($files as $file) {
    $lines = '';
    //  filename extension is '.hql'
    if (strpos($file,".hql") != false || strpos($file,".HQL") != false)
    {
        $procfile = $argv[1] . '\\' . $file;      
        echo "Converting filename: " . $procfile . "\n";
        $handle = fopen($procfile, "r");
        $lines = fread($handle, filesize($procfile));
        fclose($handle);
        $string = $lines;
        // What are we going to change runs in here
        $pattern = '[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]';
        $replacement = $sysdate;
        $lines = preg_replace($pattern, $replacement, $string);
        echo $lines;
        $newhandle = fopen($procfile, 'w+');
        fwrite($newhandle, $lines);
        fclose($newhandle);
        // DONE
    }
}

closedir($dir);

?>

When I run this code on the command line, it does not report an error and seems to work correctly. But as soon as it finishes and I check my files, I see that the contents of each file are deleted, and all of them become 0 KB files, in which there is nothing.

+4
source share
6 answers

You must surround the regular expression with a delimiter character.

:

$pattern = '![0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]!';

/ , /, !.

0

, .

(non-alphanumeric, non-backslash, non-whitespace).

/, , / .

, :

$pattern = '~[0-9]{4}/[0-9]{2}/[0-9]{2}~';

. Live demo

+3

( 1-12, 1-31)

(0(?!0)|[1-2]|3(?=[0-1]))\d\/(0(?!0)|1(?=[0-2]))\d\/\d{4}

: http://regex101.com/r/jG9nD5

+1

(# ~ , / ), 4 : yyyy/mm/dd. , . -

[0-9]{4}/[0-9]{2}/[0-9]{2}

\d{4}/\d{2}/\d{2}

... , Perl, PHP ( , "p" preg Perl, ).

0

? DateTime .

var_dump(validateDate('2012-02-28', 'Y-m-d')); # true
var_dump(validateDate('28/02/2012', 'd/m/Y')); # true
var_dump(validateDate('30/02/2012', 'd/m/Y')); # false

0

:

#!/usr/bin/php
<?php
// get the system date
$sysdate = date('d/m/Y');
// change working directory to the specified one
chdir($argv[1]);
// loop over the *.hql files in sorted order
foreach (glob('*.{hql,HQL}', GLOB_BRACE) as $file) {
    echo "Converting filename: $argv[1]\\$file\n";
    $contents = file_get_contents($file);
    $contents = preg_replace('#\d{4}/\d{2}/\d{2}#', $sysdate, $contents);
    echo $contents;
    file_put_contents($file, $contents);
}

PCRE, . .

glob file_get_contents PHP 4.3.0. file_put_contents PHP 5.

glob , , , . \\, DIRECTORY_SEPARATOR, .

file_get_contents . file_put_contents - . PHP 4, :

if (!function_exists('file_put_contents')):
function file_put_contents($filename, $data) {
    $handle = fopen($filename, 'w');
    $result = fwrite($handle, $data);
    fclose($handle);
    return $result;
}
endif;

, ?> PHP.

0

All Articles