PHP 从字符串中查找月份和年份?
PHP finding month and year from a string?
这是我迄今为止尝试从任何类型的字符串中获取年份的方法。
<?php
$variable = [
'Well year is 1967 month jan',
'year is 1876 but month January',
'HOLLA April Evening 2017',
'DOLLA-May 2020',
'OH-YESApril 2019 (IGNORE)',
'Just a sample String without year ok lets just add 1'
];
foreach ($variable as $value) {
if(ContainsNumbers($value)){
$input = preg_replace('/^[^\d]*(\d{4}).*$/', '', $value);
$input = (int)$input;
if($input>1000 && $input<2100)
{
var_dump($input);
}
}
}
function ContainsNumbers($String){
return preg_match('/\d/', $String) > 0;
}
?>
这是上面代码
的 输出
int(1967)
int(1876)
int(2017)
int(2020)
int(2019)
我现在唯一想要的就是从字符串中获取月份。
一个月来,我从这个 link 获得了帮助,但到目前为止运气不好 :-
我不知道要在 $data
中添加什么
$str = 'December 2012 Name: Jack Brown';
$ptr = "/^(?P<month>:Jan(?:uary)?|Feb(?:ruary)?|Dec(?:ember)?) (?P<year>:19[7-9]\d|2\d{3}) (Name:(?P<name>(.*)))/";
var_dumb(preg_match($ptr, $str, $data););
传统上,几乎每个人都将第三个参数称为preg_match
$matches
,所以我将在这里使用命名法。
如果 RegEx 匹配,第三个参数将是一个数组,其中包含每个项目的数字索引,以及命名索引(如果您有任何命名匹配项)。所以您正在寻找:
if(preg_match($ptr, $str, $matches)){
var_dump($matches);
}
哪个会给你:
Array
(
[0] => December 2012 Name: Jack Brown
[month] => December
[1] => December
[year] => 2012
[2] => 2012
[3] => Name: Jack Brown
[name] => Jack Brown
[4] => Jack Brown
[5] => Jack Brown
)
好吧,Chris haas 已经解释得很清楚了,但是任何人都想使用数组方法,那么请使用这个:-
<?php
$variable = [
'Well year is 1967 month jan',
'year is 1876 but month January',
'HOLLA April Evening 2017',
'DOLLA-May 2020',
'OH-YESApril 2019 (IGNORE)',
'Just a sample String without year ok lets just add 1'
];
$months = array(
'January' => 'jan',
'February' => 'feb',
'March' => 'mar',
'April' => 'apr',
'May' => 'may',
'June' => 'jun',
'July ' => 'jul',
'August' => 'aug',
'September' => 'sep',
'October' => 'oct',
'November' => 'nov',
'December' => 'dec',
);
foreach ($variable as $string) {
foreach($months as $key => $value){
if (strpos($string, $value) !== false) {
var_dump($value) ;
}
if (strpos($string, $key) !== false) {
var_dump($value) ;
}
}
}
die();
?>
输出
string(3) "jan"
string(3) "jan"
string(3) "apr"
string(3) "may"
string(3) "apr"
这是我迄今为止尝试从任何类型的字符串中获取年份的方法。
<?php
$variable = [
'Well year is 1967 month jan',
'year is 1876 but month January',
'HOLLA April Evening 2017',
'DOLLA-May 2020',
'OH-YESApril 2019 (IGNORE)',
'Just a sample String without year ok lets just add 1'
];
foreach ($variable as $value) {
if(ContainsNumbers($value)){
$input = preg_replace('/^[^\d]*(\d{4}).*$/', '', $value);
$input = (int)$input;
if($input>1000 && $input<2100)
{
var_dump($input);
}
}
}
function ContainsNumbers($String){
return preg_match('/\d/', $String) > 0;
}
?>
这是上面代码
的 输出int(1967)
int(1876)
int(2017)
int(2020)
int(2019)
我现在唯一想要的就是从字符串中获取月份。
一个月来,我从这个 link 获得了帮助,但到目前为止运气不好 :-
我不知道要在 $data
$str = 'December 2012 Name: Jack Brown';
$ptr = "/^(?P<month>:Jan(?:uary)?|Feb(?:ruary)?|Dec(?:ember)?) (?P<year>:19[7-9]\d|2\d{3}) (Name:(?P<name>(.*)))/";
var_dumb(preg_match($ptr, $str, $data););
传统上,几乎每个人都将第三个参数称为preg_match
$matches
,所以我将在这里使用命名法。
如果 RegEx 匹配,第三个参数将是一个数组,其中包含每个项目的数字索引,以及命名索引(如果您有任何命名匹配项)。所以您正在寻找:
if(preg_match($ptr, $str, $matches)){
var_dump($matches);
}
哪个会给你:
Array
(
[0] => December 2012 Name: Jack Brown
[month] => December
[1] => December
[year] => 2012
[2] => 2012
[3] => Name: Jack Brown
[name] => Jack Brown
[4] => Jack Brown
[5] => Jack Brown
)
好吧,Chris haas 已经解释得很清楚了,但是任何人都想使用数组方法,那么请使用这个:-
<?php
$variable = [
'Well year is 1967 month jan',
'year is 1876 but month January',
'HOLLA April Evening 2017',
'DOLLA-May 2020',
'OH-YESApril 2019 (IGNORE)',
'Just a sample String without year ok lets just add 1'
];
$months = array(
'January' => 'jan',
'February' => 'feb',
'March' => 'mar',
'April' => 'apr',
'May' => 'may',
'June' => 'jun',
'July ' => 'jul',
'August' => 'aug',
'September' => 'sep',
'October' => 'oct',
'November' => 'nov',
'December' => 'dec',
);
foreach ($variable as $string) {
foreach($months as $key => $value){
if (strpos($string, $value) !== false) {
var_dump($value) ;
}
if (strpos($string, $key) !== false) {
var_dump($value) ;
}
}
}
die();
?>
输出
string(3) "jan"
string(3) "jan"
string(3) "apr"
string(3) "may"
string(3) "apr"