问题
Ok, so I have a string $title_string which could look like any of the following:
$title_string = "20.08.12 First Test Event";
$title_string = "First Test event 20/08/12";
$title_string = "First Test 20.08.2012 Event";
I need to end up with two variables:
$title = "First Test Event";
$date = "20.08.12";
The formatting for the date should be converted to full-stops, regardless of what it was originally.
The Regex string that I started with looks something like this:
$regex = ".*(\d+\.\d+.\d+).*";
But I can't get this to work in the way I need it to. So all in all, I need to locate a date in a string, remove it from the string and format it correctly. Cheers.
回答1:
Matching dates with regular expressions can be quite complex. See this question for an example regex. Once you've found the date, you can remove it from the title using str_replace().
Here's a basic implementation:
$title_string = "20.08.12 First Test Event";
if ( preg_match('@(?:\s+|^)((\d{1,2})([./])(\d{1,2})\3(\d{2}|\d{4}))(?:\s+|$)@', $title_string, $matches) ) {
//Convert 2-digits years to 4-digit years.
$year = intval($matches[5]);
if ($year < 30) { //Arbitrary cutoff = 2030.
$year = 2000 + $year;
} else if ($year < 100) {
$year = 1900 + $year;
}
$date = $matches[2] . '.' . $matches[4] . '.' . $year;
$title = trim(str_replace($matches[0], ' ', $title_string));
echo $title_string, ' => ', $title, ', ', $date;
} else {
echo "Failed to parse the title.";
}
Output:
20.08.12 First Test Event => First Test Event, 20.08.2012
回答2:
<?php
#$title_string = "20.08.12 First Test Event";
#$title_string = "First Test event 20/08/12";
$title_string = "First Test 20.08.2012 Event";
preg_match('~([0-9]{1,2}[\.|/][0-9]{1,2}[\.|/][0-9]{1,4})~', $title_string, $matches);
$date = $matches[1];
$title = preg_replace('~[[:space:]]{2,}~', ' ', str_replace($date, '', $title_string));
echo 'Date: '.$date.'<br />';
echo 'Title: '.$title;
回答3:
I made some tests and this should be ok the new_title() does a replace of / by . then the preg_split splits the string when the date is met
<?php
$regex = "#(\d+[./]\d+[./]\d+)#";
print $regex . "\n\n";
print "20.08.12 First Test Event";
$title_string = new_string("20.08.12 First Test Event");
print $title_string . "\n";
$var = preg_split($regex,$title_string,-1,PREG_SPLIT_DELIM_CAPTURE);
print "result";
var_dump($var);
print "\n\n";
print "First Test event 20/08/12\n";
$title_string = new_string("First Test event 20/08/12");
print $title_string . "\n";
$var = preg_split($regex,$title_string,-1,PREG_SPLIT_DELIM_CAPTURE);
print "result";
var_dump($var);
print "\n\n";
$title_string = new_string("First Test 20.08.2012 Event");
print $title_string . "\n";
$var = preg_split($regex,$title_string,-1,PREG_SPLIT_DELIM_CAPTURE);
print "result";
var_dump($var);
function new_string($string) {
return preg_replace_callback( "#(\d+)[./](\d+)[./](\d+)#",
"new_date",
$string);
}
function new_date($matches) {
return $matches[1].'.'.$matches[2].'.'.$matches[3];
}
hope this could help
regards
来源:https://stackoverflow.com/questions/12025546/remove-date-from-string-with-regular-expressions