Regex code to convert .5 to <sup><a href="#5">5</a></sup>

I have a string containing the text of a published paper. It has footnotes, but the numbers for the footnotes simply appear at the end of the sentences like so:

Sometimes they also appear after commas. Occasionally they may appear after closing quotation marks.

Can anyone please provide a regex line of code to convert all of these numbers in $text like so?

.5 or ,5 or ”5

to

<sup><a href="#5">5</a></sup>

Thank you!

I’m no good at regex either but I’ve found this tool useful in figuring them out. https://regex101.com/

Do you want to consume the . , or " in the process?

$text = 'dsaf asdfa dsfasdfadsf.5 or dsdfsds adfsadfs,5 or fdsfdsdfs dfsdfs"5'; $new_text = preg_replace('/[.,"](\d+)/', '<sup><a href="#$0">$0</a></sup>', $text); echo '<textarea cols="100" rows="10">', $new_text, '</textarea>';
outputs

dsaf asdfa dsfasdfadsf<sup><a href="#.5">.5</a></sup> or dsdfsds adfsadfs<sup><a href="#,5">,5</a></sup> or fdsfdsdfs dfsdfs<sup><a href="#"5">"5</a></sup>

or if you don’t want to consume the ,. or ",

$new_text = preg_replace('/([.,"])(\d+)/', '$0<sup><a href="#$1">$1</a></sup>', $text);

should do it. Not tested.

$1 and $2. $0 is always “The whole original string.”

just realised that.

$new_text = preg_replace('/([.,"])(\d+)/', '$1<sup><a href="#$2">$2</a></sup>', $text);

and the first version should probably have been

$new_text = preg_replace('/[.,"](\d+)/', '<sup><a href="#$1">$1</a></sup>', $text);
1 Like

Thank you! I will try this out.

Worked great! Thanks.

Small bug, actually. Can this be updated so that instead of replacing " the code replaces &rdquo;?

Thank you.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.