Posted by Chung Leong on 12/18/06 11:50
greatprovider wrote:
> the formulas are inputted into a database in the format:
> "Na**3C**6H**5O**7*2H**20"
> i query the database and pass the list of unformated formulas through
> this function in a loop.
>
> function format_replace($formula) {
> $search = "/(\*{2}[\d]+)/";
> $replace = "<sub>\l</sub>";
> $formatted = preg_replace( $search, $replace, $formula);
> return $formatted;
> }
> the result i get for this is: Na\lC\lH\lO\l*2H\l (where "\l" is
> subscripted).
You mistyped the code I provided in a couple places. There is no need
for brackets around \d as it's already a class. You want the
parentheses around \d+, not the whole expression, since you only want
the number (and not the two asterisk). And the replacement is \1 not
\l--meaning what's inside the first pair of parentheses.
Just copy-and-paste.
[Back to original message]
|