Jump to content

Need to split title from date in field using preg_replace


Cep

Recommended Posts

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!

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.

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

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.