使用正则表达式删除特定字符串的复数词上的“s”?

时间:2014-08-06 18:47:15

标签: php regex

我有一些字符串,如" 3天前"," 6小时前"," 9分钟前"," 12秒前&# 34 ;.我是正则表达式的新手,所以我不确定如何匹配" s"之前"前"所以如果需要我可以把它剥掉。

修改:我的代码,以防它可以帮助其他人......

    function timeAgo ($timestamp) {
        $difference = time() - strtotime($timestamp);
        if ($difference > (60*60*24)) {
            $difference = round($difference/60/60/24) . " days ago";
        }
        else if ($difference > (60*60)) {
            $difference = round($difference/60/60) . " hours ago";
        }
        else if ($difference > 60) {
            $difference = round($difference/60) . " minutes ago";
        }
        else if ($difference > 0) {
            $difference = $difference . " seconds ago";
        }
        $int = filter_var($difference, FILTER_SANITIZE_NUMBER_INT);
        if ($int == 1) {
            $difference = preg_replace('/.(?= ago)/', '', $difference);
        }
        return $difference;
    }

1 个答案:

答案 0 :(得分:1)

尝试使用以下正则表达式匹配s之前的<space>ago

.(?= ago)

DEMO