Skip to content

Commit

Permalink
Merge pull request #1 from natenolting/master
Browse files Browse the repository at this point in the history
Added superscript HTML helper
  • Loading branch information
paulbunyannet committed Oct 23, 2018
2 parents 9ac0b30 + 2d43e94 commit 3eeb28e
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/Bandolier/Type/Html/SpecialCharacter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

namespace Pbc\Bandolier\Type\Html;

use Pbc\Bandolier\Type\BaseType;

class SpecialCharacter extends BaseType
{
/**
* Superscript special characters
* @see https://en.wikipedia.org/wiki/Registered_trademark_symbol#Related_symbols
* @param $string string to modify
* @param array $characters List of characters to find and apply superscript
* @return string
*/
public static function superscriptSpecialCharacters($string, array $characters = []) {

$defaults = [
'registered' => ['®', '&reg;', '&#x00AE;', '&#174;'],
'trademark' => ['™', '&trade;', '&#8482;'],
'copyright' => ['©', '&#169;', '&copy;'],
'service' => ['℠', '&#8480;'],
'phonogram' => ['℗', '&#8471;'],
'hechsher' => ['Ⓤ', '&#9418;']
];
$characters = array_merge($defaults, $characters);
$specials = array();
array_walk_recursive($characters, function ($v) use (&$specials) {
$specials[] = $v;
});
// wrap all the special characters above with superscript tags
for ($i = 0, $iCount = count($specials); $i < $iCount; $i++) {
if (strpos($string, $specials[0]) !== false) {
$string = str_replace($specials[$i], '<sup>' . $specials[$i] . '</sup>', $string);
}
}

return $string;
}
}
26 changes: 26 additions & 0 deletions tests/Type/Html/SpecialCharacterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
/**
* ${CLASS_NAME}
*
* Created 10/23/18 1:16 PM
* Description of this file here....
*
* @author Nate Nolting <naten@paulbunyan.net>
* @package Pbc\Bandolier\Type\Html
* @subpackage Subpackage
*/

namespace Pbc\Bandolier\Type\Html;


class SpecialCharacterTest extends \PHPUnit_Framework_TestCase
{

public function testSuperscriptSpecialCharacters()
{
$string = "This® Thing™ is©";
$stringAfter = "This<sup>®</sup> Thing<sup>™</sup> is<sup>©</sup>";

$this->assertSame($stringAfter, SpecialCharacter::superscriptSpecialCharacters($string));
}
}

0 comments on commit 3eeb28e

Please sign in to comment.