示例字符串:
这是一个包含一些测试用例的示例字符串 ''cite{author1,author2}。然后 {另一个示例} ''citeauthor{author3} 和最后 ''citeP{author1} 和字符串在这里完成。
要求: 1.我需要全局提取 {} 之间但在"''cite"字符串之后的内容。 (例如 ''cite{..} 或 ''citeauthor{..})
我试过:preg_match('/[^''''cite]{(.*?)}/', $text, $match);//dint work
2.使用基于半升提取的字符串,字符串将被分解。(eg. if extracted string="author1,author2")
,那么它将根据","字符)//it will be done with explode()
进行爆炸
3.如果大括号来自('cite or 'citeauthor etc)
,则应将其替换为for eg. <xref cite>exploded string</xref>
,因为我想知道字符串是否来自"cite" or "citeauthor" or "citeP".
4.如果爆炸的字符串是第一次出现,我想替换字符串。 For eg. <xref cite 1>exploded string</xref>
预期产出:这是一个示例字符串,其中包含一些测试用例<xref cite 1>author1</xref><xref cite 1>author2</xref>.
然后 {另一个示例} <xref citeauthor 1>author3</xref>
,最后<xref citeP>author2</xref>
和字符串在此处完成。
那么,如何获取 {} 之间有字符串的('cite or 'citep or 'citeauthor)
以及字符串在文件中的第一次出现。
output= <xref cite 1>exploded string</xref>
- 如果我在 'cite
之后只得到 {} 之间的字符串,那么我怎么能在每次出现时也得到"'cite"
字符串。
感谢您的阅读。对不起,大帖子,我想这是可以理解
preg_replace_callback
:
$s = 'This is a example string with some testing cases 'cite{author1,author2}.
Then {another example} 'citeauthor{author3} and finally
'citeP{author1} and string completes here.';
echo preg_replace_callback('/''''(cite(?:P|author)?){([^}]*)}/', function($m) {
return preg_replace('/([^,]+)(?:,|$)/' ,'<xref '. $m[1] . ' 1>$1</xref>', $m[2]); },
$s);
输出
This is a example string with some testing cases <xref cite 1>author1</xref><xref cite 1>author2</xref>. Then {another example} <xref citeauthor 1>author3</xref> and finally <xref citeP 1>author1</xref> and string completes here.