Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added superscript HTML helper #1

Merged
merged 1 commit into from
Oct 23, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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));
}
}