[英]PHP - preg_match?
I'm not very good with expressions... I've looked at some online tutorials, but I'm still not getting it.我不太擅长表达......我看过一些在线教程,但我仍然不明白。 Basically, I'm trying to return
TRUE
if a string is formatted like this:基本上,如果字符串的格式如下,我会尝试返回
TRUE
:
4 digits + space + 2 digits and convert it to a date. 4 位数字 + 空格 + 2 位数字并将其转换为日期。
So, the string will look like: 2010 02
, and I'm trying to output February, 2010
.所以,字符串看起来像:
2010 02
,我试图输出February, 2010
。
I'm trying to use preg_match
, but I keep getting我正在尝试使用
preg_match
,但我不断收到
{ is not a modifier...
{ 不是修饰符...
EDIT编辑
Per the first 2 responses, I changed it, but am getting a fatal error on the first and the same unknown modifier error on the second:根据前 2 个响应,我更改了它,但在第一个和第二个相同的未知修饰符错误上遇到致命错误:
if (preg_match('/([0-9{4}]) ([0-9]{2})/iU', $path_part)) {
$path_title = date("F, Y",strtotime(str_replace(" ", "-", $path_title)));
}
Also, just tried the more in-depth version in the first response, and while the error goes away, it doesn't change the output:此外,刚刚在第一个响应中尝试了更深入的版本,虽然错误消失了,但它不会改变输出:
$path_part = '2010 02';
if (preg_match('/^(\d{4}) (\d{2})$/', $path_part, $matches)) {
$path_title = $mon[(int)$matches[2]] . " " . $matches[1]; // prints Feb 2010
}
I'm trying to return TRUE if a string is formatted like this: 4 digits + space + 2 digits如果字符串的格式如下,我试图返回 TRUE:4 位数字 + 空格 + 2 位数字
return preg_match(/^\d{4} \d{2}$/,$input);
To convert to date you can try something like:要转换为日期,您可以尝试以下操作:
$mon = array('','Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
$date_str = "2010 02";
if(preg_match('/^(\d{4}) (\d{2})$/',$date_str,$matches))
{
print $mon[(int)$matches[2]] . " " . $matches[1]; // prints Feb 2010
}
$in = "2010 02";
if(preg_match('/([0-9]{4}) ([0-9]{2})/i', $in, $matches)) {
echo date("F Y", strtotime($matches[2] . "/1/" . $matches[1]));
}
试试这个...
preg_match('/([0-9{4}]) ([0-9]{2})/iU', $input);
Without having any detail as to your actual code, the following should work:在没有关于您的实际代码的任何细节的情况下,以下应该有效:
<?php
$str = '2010 02';
$months = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December');
if(preg_match('/([0-9]{4}) ([0-9]{2})/', $str, $match) == 1){
$year = $match[1];
$month = (int) $match[2];
echo $months[$month - 1] . ', ' . $year;
}else{
//Error...
}
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.