【问题标题】:How to strip unix timestamps from a string PHP [closed]如何从字符串 PHP 中去除 unix 时间戳 [关闭]
【发布时间】:2013-07-17 06:28:50
【问题描述】:

假设我有以下字符串:

$string = 'Th1s 1s my 5tr1ng with a timestamp 1357002120 and another timestamp here 1357002120.';

我想要的是返回一个字符串,没有两个时间戳,但保留任何其他数字。

所以,输出应该是:

'Th1s 1s my 5tr1ng with a timestamp and another timestamp here.'

我认为某种正则表达式可以做到这一点,但我不知道从哪里开始。

【问题讨论】:

  • 我们如何确定另一个数字不是timestamp
  • 如何定义 UNIX 时间戳?只是任何被单词边界包围的数字?因为1-40 也是UNIX 时间戳。
  • 好点,老实说我没有考虑过。应用程序中的要求范围是时间戳必须在最近 7 天内。所以,我想我们可以安全地假设它是 10 位长,为了争论,我们假设每 10 或 11 位长的数值都是这个变量中的一个时间戳。

标签: php regex unix timestamp


【解决方案1】:

您可以稍后自行开发的简单解决方案:

<?php
$string = 'Th1s 1s my 5tr1ng with a timestamp 1357002120 and another timestamp here 1357002120.';
echo preg_replace("#\d{10,11}#", "", $string);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2021-03-21
    • 2015-10-22
    • 1970-01-01
    • 2016-08-31
    • 1970-01-01
    • 2018-10-17
    相关资源
    最近更新 更多