【问题标题】:How to parse information not using explode?如何解析不使用爆炸的信息?
【发布时间】:2014-10-29 15:57:19
【问题描述】:

我有一些使用 Wikipedia API 从 Wikipedia Infobox 解析的信息。我要解析的信息之一是生日:

$birthdate = "birth_date = {{Birth date|1958|8|29}}"

我想要输出如下所示的 birtdate 信息:

1958-8-29

在我的考虑中,我可以使用 php 函数 explode,但我认为这是在每种情况下获取正确信息的一种非常糟糕的方法。

我的问题是:

有没有人知道在每种情况下都可以独立于其他内容来解析生日信息的方法? 如果是的话,你能举个例子吗?

【问题讨论】:

标签: php parsing explode wikipedia wikipedia-api


【解决方案1】:

你可以用正则表达式试试这个。

$matches = array();
$birthdate = "birth_date = {{Birth date|1958|8|29}}";
preg_match('/^(.*)\|([0-9]{4})\|([0-9]{1,2})\|([0-9]{1,2})}}/is', $birthdate, $matches);
if (!empty($matches[2]) && !empty($matches[3]) && !empty($matches[4])) {
    echo $matches[2]."-".$matches[3]."-".$matches[4];
} else {
    echo 'Not found the parts of the date';
}
//Output is: 1958-8-29 

【讨论】:

    猜你喜欢
    • 2014-10-17
    • 2012-01-23
    • 1970-01-01
    • 1970-01-01
    • 2021-06-21
    • 2011-10-05
    • 2021-09-20
    • 1970-01-01
    相关资源
    最近更新 更多