Jump to content

preg_replace part of string


ejaboneta

Recommended Posts

I'm trying to fix a bunch of formatting errors that resulted in 1900 dates being converted into 2000 dates...

 

I'm trying to change 12/20/2012 to 12/20/1912. I can replace any /20 with /19 but I dont any days to be mixed up and being changed from 12/20/2012 to 12/19/1912.

 

I tried doing "/20.{2}$" but of course it I'll end up with 12/20/19. Sorry I'm new to regex so I am pretty lost.

 

<?php
      $data = '12/20/2012';

      $birthstr = "/\/20/";
      $birthrep = '/19';
      $birthday = preg_replace($birthstr,$birthrep,$data);
?>

Link to comment
https://forums.phpfreaks.com/topic/177678-preg_replace-part-of-string/
Share on other sites

Probably not the best solution, but it's late, so off the top of my head....

 

preg_replace("~(\d\d/\d\d)/20(\d\d)~", "$1"."/19"."$2", $input);

 

you don't need those first 4 \d's since your pattern is expecting the 2 \d's after the 20.

 

$input = preg_replace("~/20(\d\d)~", "/19$1", $input);

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.