Jump to content

regex to help me style superscripts


peppericious

Recommended Posts

I want to mark text snippets with superscript numbering. The numbering will refer the reader to corresponding footnotes later.

 

In my text, I want to quickly enter the superscripts like this:

*1*

*2*

*3*

*4*

...

*9*

*10*

*11*

.. etc.

 

I want to wrap the superscripts in a span tag to style them.

 

So, my question: how to I replace the opening and closing asterisks so that I'll end up with:

<span class='superscript'>1</span>
<span class='superscript'>2</span>
<span class='superscript'>3</span>
<span class='superscript'>9</span>
...
<span class='superscript'>10</span>
<span class='superscript'>11</span>

 

Given the content I'm working on, it is highly unlikely that the superscript number will ever be longer than 2 digits.

 

Thanks in advance for your help.

Link to comment
https://forums.phpfreaks.com/topic/262828-regex-to-help-me-style-superscripts/
Share on other sites

To start with,

/\*(\d+)\*/

 

I spoke too soon... I can't get this to work.

 

What you have given me is replacement pattern I should use in conjunction with the preg_replace function, is that right? I am finding it difficult to specify the original pattern...

Got it!...

 

 

<html>
<head>
<style type='text/css'>
.ss {
color:red;
}
</style>
</head>
<body>
<?php
// style superscripts

$str = "Testing*1* one two three";
$tosearchfor = "/\*(\d+)\*/";
$toreplacewith = "<span class='ss'>$1</span>";

echo preg_replace($tosearchfor, $toreplacewith, $str);
?>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.