一个正则表达式会有效, 因为正则表达式是为此而设计的: 选择基于模式的数据 。 但是, 您可以在, (comma) 上爆炸, 然后再将前四个元素一起插入, 以形成您的句子 。 我怀疑在此情况下使用正则表达式会更快 。
Ultimately it s your preference: which is better readable and understandable by you.
The main advantage regular expression would have in this particular case is hat they can extract specific values/patterns, so you could easily have them set aside the month for instance.
$dateString = "On Sun, May 27, 2012 at 6:25 AM, some other text here";
// using explode/implode
$result = explode( , ,$dateString);
print "we got: " . implode( , , array_slice($result,0,3)) . "
";
// using regular expression
$pattern = "/On [A-Z,a-z]{3}, [A-Z,a-z]{3} [0-9]+, [0-9]{4} at [0-9,:]+ (?:A|P)M/U";
preg_match($pattern,$dateString,$match);
print "We got: " . $match[0] . "
";
也请阅读 rel=“nofollow”>PHP手册,定期表达小节,加上首页http://www.grymoire.com/Unix/Regular.html" rel=“nofollow”>tument
就个人而言,我认为正方字的表达方式在视觉和性能上都可能超乎想象。 但是,要学习常规表达方式,它们有时会很有帮助。