【问题标题】:Remove date from string with regular expressions使用正则表达式从字符串中删除日期
【发布时间】:2012-08-19 09:38:48
【问题描述】:

好的,所以我有一个字符串 $title_string,它可能看起来像以下任何一种:

$title_string = "20.08.12 First Test Event";
$title_string = "First Test event 20/08/12";
$title_string = "First Test 20.08.2012 Event";

我需要得到两个变量:

$title = "First Test Event";
$date = "20.08.12";

日期的格式应该转换为句号,不管它最初是什么。

我开始使用的 Regex 字符串看起来像这样:

$regex = ".*(\d+\.\d+.\d+).*";

但我无法让它以我需要的方式工作。所以总而言之,我需要在字符串中找到一个日期,将其从字符串中删除并正确格式化。干杯。

【问题讨论】:

标签: php regex


【解决方案1】:

使用正则表达式匹配日期可能非常复杂。有关示例正则表达式,请参阅 this question。找到日期后,您可以使用 str_replace() 将其从标题中删除。

这是一个基本的实现:

$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.";
}

输出:

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】:

    我做了一些测试,应该没问题 new_title() 将 / 替换为 。然后 preg_split 在满足日期时拆分字符串

    <?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];
    }
    

    希望这会有所帮助

    问候

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-12-21
      • 2013-05-23
      • 1970-01-01
      • 2021-01-17
      • 2014-06-20
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      相关资源
      最近更新 更多