user4295236
user4295236

Reputation:

preg_match all the first occurrences and get two matches in a file

Example string:

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.

Requirement: 1. I need to extract the content between {} but after "\cite" string globally. (eg. \cite{..} or \citeauthor{..})

I tried:preg_match('/[^\\\\cite]{(.*?)}/', $text, $match);//dint work

2.With the extracted string based on demiliter the string will be exploded.(eg. if extracted string="author1,author2") ,then it will be exploded based on "," charac)
//it will be done with explode()

3.If the curly braces is from (\cite or \citeauthor etc), it should be replaced with for eg. <xref cite>exploded string</xref> because I want to know whether the string is from "cite" or "citeauthor" or "citeP".

4.And I want to replace the string if the exploded string is the first occurrence. For eg. <xref cite 1>exploded string</xref>

Expected output: 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>author2</xref> and string completes here.

So,How to get (\cite or \citep or \citeauthor) which has a string between {} and also the first occurrence of the string in a file.

output=<xref cite 1>exploded string</xref> - if I get only string between {} after \cite then how can I get "\cite" string also with that for every occurrence.

Thanks for reading. Sorry for the big post, I think it will be understandable

Upvotes: 3

Views: 70

Answers (1)

anubhava
anubhava

Reputation: 785651

You can use 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);

Output

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.

Upvotes: 1

Related Questions