2013-02-13 44 views
2

我正在循環瀏覽一些帶有嵌入式文獻參考的文本。其中一些是DOI號,我需要將它們聯繫起來。使用preg_replace鏈接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:更新的DOI解析器根據卡特琳的答案在下面。

回答

0

使用Regular Expression Test Tool我發現一個expression,對於我的例子文字作品:

$pattern  = '(10[.][0-9]{4,}[^\s"/<>]*/[^\s"<>]+)'; 
$replacement = "<a href='http://dx.doi.org/$0' target='1'>doi:$0</a>"; 
$html = preg_replace($pattern, $replacement, $html); 

心連心