从php字符串捕获日期短语
我有字符串(在数组中):从php字符串捕获日期短语,php,Php,我有字符串(在数组中): $a=“账户电话48201389user@whatever.net于2013年7月1日在JHB发布。 及 $b=“2013-08-11在PE中安装”。 我只需要使用PHP获取每个字符串的完整日期。 是否可以将通配符与pregmatch一起使用? 我试过: preg_match('/(?P\w+):(?P'\d+/”,$str,$matches); 但它给出了一个错误。 最终结果应该是:$a=2013-07-01“和$b=“2013-08-11” 谢谢 应适用于两个字符
$a=“账户电话48201389user@whatever.net于2013年7月1日在JHB发布。
及
$b=“2013-08-11在PE中安装”。
我只需要使用PHP获取每个字符串的完整日期。是否可以将通配符与pregmatch一起使用?
我试过:
preg_match('/(?P\w+):(?P'\d+/”,$str,$matches);
但它给出了一个错误。最终结果应该是:
$a=2013-07-01“
和$b=“2013-08-11”
谢谢
应适用于两个字符串-如果日期始终采用此格式。您可以使用preg\u match\u all获取字符串中的所有日期模式。所有字符串匹配将保存在数组中,该数组应作为参数传递给函数 在本例中,将所有模式dd保存在数组$matches中
$string = "account Tel48201389 user@whatever.net dated 2013-07-01 in JHB installation on 2013-08-11 in PE";
if (preg_match_all("@\d{4}-\d{2}-\d{2}@", $string, $matches)) {
print_r($matches);
}
祝你好运!你可以做到这一点
<?php
$b = 'installation on 2013-08-11 in PE';
preg_match('#([0-9]{4}-[0-9]{2}-[0-9]{2})#', $b, $matches);
if (count($matches) == 1) {
$b = $matches[0];
echo $b; # 2013-08-11
}
?>
试试这个
$a = "account Tel48201389 user@whatever.net dated 2013-07-01 in JHB";
preg_match("/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/", $a, $matches);
if($matches){
echo $matches[0];// For the complete string
echo $matches['year'];//for just the year etc
}
$a=“账户电话48201389user@whatever.net于2013年7月1日在金华银行登记”;
preg_match(“/(?P[0-9]{4})-(?P[0-9]{2})-(?P[0-9]{2})/”,$a,$matches);
如果($匹配){
echo$匹配[0];//用于完整字符串
echo$matches['year'];//仅用于该年等
}
“它给出了一个错误”…你愿意分享你得到的错误吗?我希望你不要用你在这里发布的方式编写代码。(我的意思是可读性)现在你有一个数组还是有两个变量$a
和$b
?那些'
在用'分隔的字符串中做什么?(如果没有撇号,我无法上传到stackoverflow。三角括号使角括号之间的名称和数字消失,对此表示抱歉。)哇,看起来很酷。谢谢。echo$matches[0][0]。“,”$matches[0][1]。“\n”;
<?php
$b = 'installation on 2013-08-11 in PE';
preg_match('#([0-9]{4}-[0-9]{2}-[0-9]{2})#', $b, $matches);
if (count($matches) == 1) {
$b = $matches[0];
echo $b; # 2013-08-11
}
?>
$a = "account Tel48201389 user@whatever.net dated 2013-07-01 in JHB";
preg_match("/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/", $a, $matches);
if($matches){
echo $matches[0];// For the complete string
echo $matches['year'];//for just the year etc
}