我正在循环浏览一些带有嵌入式文献引用的文本。其中一些是 DOI 号,我需要将它们链接起来。
示例文本:
<div>Interesting article here: doi:10.1203/00006450-199305000-00005</div>
到目前为止我尝试过的:
$html = preg_replace("\b(10[.][0-9]{4,}(?:[.][0-9]+)*/(?:(?![\"&\'<>])[[:graph:]])+)\b", "<a href='https://doi.org/\\0' target='_new'>doi:\\0</a>",$html);
这将返回一个空字符串。
我期待:
<div>Interesting article here: <a href='https://doi.org/10.1203/00006450-199305000-00005' target='_new'>doi:10.1203/00006450-199305000-00005</a></div>
我哪里出错了?
编辑 2018-01-30:根据下面 Katrin 的回答更新了 DOI 解析器。
最佳答案
CrossRef has a recommendation ,他们在 99.3% 的 DOI 上测试成功:
/^10.\d{4,9}/[-._;()/:A-Z0-9]+$/i
关于regex - 使用 preg_replace 链接 DOI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14861893/