Skip to content
Advertisement

Wrap Latex Equation using custom tag PHP regex

I just want to wrap the Math latex equation using any html tag. The string be like

y = x2  or (x = sqrt{y}) . For all (y geq 0), x is defined. Thus, y is set of all non-negative and real number. Hence, range is (y geq 0) or [0, ∞).

I want output like

y = x2  or <math>(x = sqrt{y})</math> . For all <math>(y geq 0)</math>, x is defined. Thus, y is set of all non-negative and real number. Hence, range is <math>(y geq 0)</math> or [0, ∞).

Advertisement

Answer

The pattern for equation seems like this:

((\()((?!(\()|(\))).)*(\)))

The prefix (\() and suffix (\)) are simple check for start and end of the equation.

The middle part ((?!(\()|(\))).)* count any character (.) (except n) unless by looking ahead “(?! ...)” the two character in front be one of ( or ).

The whole equation pattern are in (...), so it could be referred to as group1 or $1. So that should be substituted by:

<math>$1</math>

Link to test for PHP PCRE2

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement