The14thGOD Posted October 26, 2009 Share Posted October 26, 2009 I'm updating a database from a CSV file, for some reason the database isn't putting quotes around the dates, but it breaks if the data doesn't have quotes around it. Now I originally just targeted those fields in an array and added the quotes (e.g. $arr[22] = '\''.$arr[22].'\'' and that worked until it got to a field with no date in it. That broke that so I had to go back to trying to just find a regex to add the quotes (and hope that the insert works if there is no date and no quotes). Here's the regex I wrote (I'm by no means an expert on this) and it doesn't work so any help would be great; <?php $data = preg_replace('^([0-9]{4}-[0-9]{2}-[0-9] [0-9]{2}:[0-9]{2}:[0-9]{2})^','\'\\1\'',$data); ?> All dates (except ones that don't exist) follow that pattern since it's stored in a MySQL db. Thanks for any and all help, Justin Quote Link to comment https://forums.phpfreaks.com/topic/179118-preg_replace-mysql-date/ Share on other sites More sharing options...
salathe Posted October 26, 2009 Share Posted October 26, 2009 Are you sure that all dates follow that pattern? The day number is only allowed one digit. Quote Link to comment https://forums.phpfreaks.com/topic/179118-preg_replace-mysql-date/#findComment-945042 Share on other sites More sharing options...
The14thGOD Posted October 26, 2009 Author Share Posted October 26, 2009 ah crap....missed that one...lol looks like theres another error though, I think the CSV file is just too messed up... thanks for noticing that! Quote Link to comment https://forums.phpfreaks.com/topic/179118-preg_replace-mysql-date/#findComment-945044 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.