Cep Posted April 5, 2007 Share Posted April 5, 2007 I am building a CSV file but one of the fields for titles contains the title and a date at the end in the format of dd/mm/yy, such as "summer manual 03/01/07". I want to split this into two csv fields by doing this, "summer manual,03/01/07". What regex do you need to search for the dd/mm/yy part of the string? I am not very good at these myself. Thanks in advance! Link to comment https://forums.phpfreaks.com/topic/45736-need-to-split-title-from-date-in-field-using-preg_replace/ Share on other sites More sharing options...
pocobueno1388 Posted April 5, 2007 Share Posted April 5, 2007 You can just use explode to split them up. <?php $var = "summer manual 03/01/07"; $explode = explode(" ", $var); echo "$explode[0] $explode[1]"; // This prints "summer manual" echo '<br>'; echo $explode[2]; // This holds the date ?> The code is untested...but it should work. Link to comment https://forums.phpfreaks.com/topic/45736-need-to-split-title-from-date-in-field-using-preg_replace/#findComment-222217 Share on other sites More sharing options...
kenrbnsn Posted April 5, 2007 Share Posted April 5, 2007 That solution assumes that there will always be exactly 3 words in the string. It could be done using a negative starting value for the substr() function. This solution assumes that the dete would always be entered as mm/dd/yy. <?php $var = "summer manual 03/01/07"; $datepart = substr($var,-; $textpart = substr($var,0,strlen($var)-; $newvar = trim($textpart) . ',' . $datepart; ?> Ken Link to comment https://forums.phpfreaks.com/topic/45736-need-to-split-title-from-date-in-field-using-preg_replace/#findComment-222222 Share on other sites More sharing options...
effigy Posted April 5, 2007 Share Posted April 5, 2007 <pre> <?php $string = 'summer manual 03/01/07'; $pieces = preg_split('%\s+(?=\d{1,2}/\d{1,2}/\d{2,4})%', $string); print_r($pieces); ?> </pre> Link to comment https://forums.phpfreaks.com/topic/45736-need-to-split-title-from-date-in-field-using-preg_replace/#findComment-222229 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.