preg_replace 和日期

发布于 2024-09-27 08:54:20 字数 228 浏览 3 评论 0原文

我想删除日期字符串中的所有 -/ 字符。有人可以帮我吗?

这是我所拥有的,但它不起作用。

preg_replace('/','',$date);
preg_replace('-','',$date);

另外,有没有办法将这两个表达式组合在一起,这样我就不必有 2 个 preg_replaces 了?

I want to remove all - and / characters in a date string. Can someone give me a hand?

Here is what I have but it doesn't work.

preg_replace('/','',$date);
preg_replace('-','',$date);

Also, is there a way to group these two expressions together so I don't have to have 2 preg_replaces?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

执妄 2024-10-04 08:54:20

使用 $date = str_replace(aray('/','-'),'',$date); 也快得多。

use $date = str_replace(aray('/','-'),'',$date); It's also much faster.

恬淡成诗 2024-10-04 08:54:20

使用“翻译”方法代替正则表达式。在 PHP 中,这将是 strtr()

strtr( $date, '/-', '' );

Instead of a regex, use a 'translate' method. In PHP, that would be strtr()

strtr( $date, '/-', '' );
不…忘初心 2024-10-04 08:54:20

是的!您需要仔细查看 手动

以下是使用 preg_replace() 的示例:

#!/usr/bin/env php
<?
$date = "2009/08/07";
echo "Before: ${date}\n";
$date = preg_replace('/[-\/]/', '', $date);
echo "After: ${date}\n";
$date = "2009-08-07";
echo "Before: ${date}\n";
$date = preg_replace('/[-\/]/', '', $date);
echo "After: ${date}\n";
?>


% ./test.php 
Before: 2009/08/07
After: 20090807
Before: 2009-08-07
After: 20090807

Yes! You need to take a closer look at the examples of $pattern in the manual.

Here's an example using preg_replace():

#!/usr/bin/env php
<?
$date = "2009/08/07";
echo "Before: ${date}\n";
$date = preg_replace('/[-\/]/', '', $date);
echo "After: ${date}\n";
$date = "2009-08-07";
echo "Before: ${date}\n";
$date = preg_replace('/[-\/]/', '', $date);
echo "After: ${date}\n";
?>


% ./test.php 
Before: 2009/08/07
After: 20090807
Before: 2009-08-07
After: 20090807
最单纯的乌龟 2024-10-04 08:54:20

我认为 [/\-] 是最有效的。

[/\-] is the most efficient, I think.

荒芜了季节 2024-10-04 08:54:20

由于您要将一个字符替换为另一个字符,因此基于正则表达式的解决方案是一种矫枉过正。您可以使用 str_replace 如下

$edited_date = str_replace(array('/','-'),'',$date);

:您的 preg_replace 出了什么问题?

preg_replace 期望正则表达式被一对分隔符包围。所以这应该有效:

$edited_date = preg_replace('#-#','',$date);

同样,与 str_replace 一样,preg_replace 也接受数组,因此您可以这样做:

$from = array('#/#','#-#');
$to = '';
$edited_date = preg_replace($from,$to,$date);

您还可以将要在单个正则表达式中删除的两个模式组合起来,如下所示:

$edited_date = preg_replace('#-|/#','',$date);

Since you are replacing one character with another character, a regex based solution is an overkill. You can just use str_replace as:

$edited_date = str_replace(array('/','-'),'',$date);

Now what was wrong with your preg_replace ?

preg_replace expects the regex to be surrounded by a pair of delimiters. So this should have worked:

$edited_date = preg_replace('#-#','',$date);

Also as str_replace, preg_replace also accepts arrays, so you can do:

$from = array('#/#','#-#');
$to = '';
$edited_date = preg_replace($from,$to,$date);

Also you can combine the two patterns to be removed in a single regex as:

$edited_date = preg_replace('#-|/#','',$date);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文