-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from natenolting/master
Added superscript HTML helper
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' => ['®', '®', '®', '®'], | ||
'trademark' => ['™', '™', '™'], | ||
'copyright' => ['©', '©', '©'], | ||
'service' => ['℠', '℠'], | ||
'phonogram' => ['℗', '℗'], | ||
'hechsher' => ['Ⓤ', 'Ⓤ'] | ||
]; | ||
$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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); | ||
} | ||
} |