HTML Superscript to Plain Text with PHP

1.6k views Asked by At

I am working on converting HTML document to plain text by striping and replacing all the HTML tags and succeed in doing so. But i have come across this situation where i need to handle the superscript. I have this HTML code :

11,500m²

(suppose there are sup tag shown above, but i don't know how to show them here) I need to convert it to plain text so that it will become just 11,500m². How can i do so? Thank you in advance.

1

There are 1 answers

3
fjoe On BEST ANSWER

As there are only a few superscript numbers in ASCII.

// replace all ... things to a power of 1
str_replace("<sup>1</sup>", "¹", $html)
// replace all squares
str_replace("<sup>2</sup>", "²", $html)
// replace all cubes
str_replace("<sup>3</sup>", "³", $html)
// for everything else use ^ notation
str_replace("<sup>", "^", $html)
// remove leftover closing sup tags
str_replace("</sup>", "", $html)

As there is no way in plain text to have most characters this solution will:

Find text like: Some TextOther

And output: Some Text^Other