Skip to content

Instantly share code, notes, and snippets.

@borsna
Last active September 26, 2024 13:50
Show Gist options
  • Save borsna/fb13012ce87d47080898aed793a86d7f to your computer and use it in GitHub Desktop.
Save borsna/fb13012ce87d47080898aed793a86d7f to your computer and use it in GitHub Desktop.
PHP string to subscript and superscript string
<?php
/**** example usage ****
print SubSupText::replaceSubSupHtml("H<sub>2</sub>O") . PHP_EOL; //H₂O
print SubSupText::replaceSubSupHtml("42km<sup>2</sup>") . PHP_EOL; //42km²
print SubSupText::replaceSubSupHtml("2<sup>th</sup> chapter") . PHP_EOL; //2ᵗʰ chapter
*/
class SubSupText{
const superscriptMap = [
'0' => '⁰', '1' => '¹', '2' => '²', '3' => '³', '4' => '⁴', '5' => '⁵',
'6' => '⁶', '7' => '⁷', '8' => '⁸', '9' => '⁹', 'a' => 'ᵃ', 'b' => 'ᵇ',
'c' => 'ᶜ', 'd' => 'ᵈ', 'e' => 'ᵉ', 'f' => 'ᶠ', 'g' => 'ᵍ', 'h' => 'ʰ',
'i' => 'ⁱ', 'j' => 'ʲ', 'k' => 'ᵏ', 'l' => 'ˡ', 'm' => 'ᵐ', 'n' => 'ⁿ',
'o' => 'ᵒ', 'p' => 'ᵖ', 'q' => 'q', 'r' => 'ʳ', 's' => 'ˢ', 't' => 'ᵗ',
'u' => 'ᵘ', 'v' => 'ᵛ', 'w' => 'ʷ', 'x' => 'ˣ', 'y' => 'ʸ', 'z' => 'ᶻ',
];
const subscriptMap = [
'0' => '₀', '1' => '₁', '2' => '₂', '3' => '₃', '4' => '₄', '5' => '₅',
'6' => '₆', '7' => '₇', '8' => '₈', '9' => '₉', 'a' => 'ₐ', 'e' => 'ₑ',
'h' => 'ₕ', 'i' => 'ᵢ', 'j' => 'ⱼ', 'k' => 'ₖ', 'l' => 'ₗ', 'm' => 'ₘ',
'n' => 'ₙ', 'o' => 'ₒ', 'p' => 'ₚ', 'r' => 'ᵣ', 's' => 'ₛ', 't' => 'ₜ',
'u' => 'ᵤ', 'v' => 'ᵥ', 'x' => 'ₓ', 'y' => 'ᵧ', 'z' => '𝓏',
];
public static function toSuperscript(string $str): string{
$outputString = '';
$length = mb_strlen($str, 'UTF-8');
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($str, $i, 1, 'UTF-8');
$outputString .= isset(self::superscriptMap[$char]) ? self::superscriptMap[$char] : $char;
}
return $outputString;
}
public static function toSubscript(string $str): string{
$outputString = '';
$length = mb_strlen($str, 'UTF-8');
for ($i = 0; $i < $length; $i++) {
$char = mb_substr($str, $i, 1, 'UTF-8');
$outputString .= isset(self::subscriptMap[$char]) ? self::subscriptMap[$char] : $char;
}
return $outputString;
}
public static function replaceSubSupHtml(string $htmlString): string {
$supPattern = '/<sup>(.*?)<\/sup>/s';
$htmlString = preg_replace_callback($supPattern, function ($matches) {
return self::toSuperscript($matches[1]);
}, $htmlString);
$subPattern = '/<sub>(.*?)<\/sub>/s';
return preg_replace_callback($subPattern, function ($matches) {
return self::toSubscript($matches[1]);
}, $htmlString);
}
}
@borsna
Copy link
Author

borsna commented Feb 2, 2024

not complete at all just a quick hack to fix some html strings to be unicode only.
might be useful to someone.
if you come up with any improvements please write a comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment