Last active
December 23, 2015 18:19
-
-
Save faffyman/6674946 to your computer and use it in GitHub Desktop.
A view helper for ZF2 (Zend Framework 2) This view helper will "urlize" a word or phrase to make it "safer" as a url slug. e.g. It's Séan's Party becomes its-seans-party
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
<?php | |
namespace Application\View\Helper; | |
use Zend\View\Helper\AbstractHelper; | |
class Urlize extends AbstractHelper | |
{ | |
public function __invoke($string) | |
{ | |
// Unicode? - don't allow accented characters in our output | |
// e.g. "It's Séan's Party" becomes "its-seans-party" | |
// Funky Quotes | |
$quotes_array = array("“" => "\"", "”" => "\"" ); | |
$string = strtr($string, $quotes_array); | |
// Accented Characters | |
$string = strtr($string, array( | |
"æ" => "ae", "Æ" => "AE", "œ" => "oe", "Œ" => "OE", | |
"ß" => "ss", "£" => "pound", "€" => "euro", | |
"à" => "a", "á" => "a", "â" => "a", "ä" => "a", "å" => "a", | |
"À" => "A", "Ã" => "A", "Á" => "A", "Â" => "A", | |
"ç" => "c", "Ç" => "C", | |
"ê" => "e", "ë" => "e", "è" => "e", "é" => "e", | |
"Ê" => "E", "Ë" => "E", "É" => "E", "È" => "E", | |
"ï" => "i", "í" => "i", "î" => "i", "ì" => "i", | |
"Í" => "I", "Ì" => "I", "Î" => "I", "Ï" => "I", | |
"ñ" => "n", "Ñ" => "N", | |
"ô" => "o", "ö" => "o", "ò" => "o", "õ" => "o", "ó" => "o", "ø" => "o", | |
"Ó" => "O", "Ô" => "O", "Õ" => "O", "Ø" => "O", "Ö" => "O", "Ò" => "O", | |
"š" => "s", "Š" => "S", | |
"ú" => "u", "ü" => "u", "û" => "u", "ù" => "u", | |
"Ù" => "U", "Ú" => "U", "Ü" => "U", "Û" => "U", | |
"ÿ" => "y", "Ÿ" => "Y", "ý" => "y", "Ý" => "Y", | |
"ž" => "z", "Ž" => "Z", | |
"þ" => "b", "ƒ" => "f", "µ" => "u", "ð" => "o", "Ð" => "D", "¥" => "Y" | |
) ); | |
// Lowercase | |
$string = strtolower($string); | |
// replaces spaces with dashes | |
$string = str_replace(' ','-',$string); | |
// Ampersands become the word and | |
$string = str_replace('&', 'and', $string); | |
// Quotes get dropped | |
$string = str_replace("'", '', $string); | |
$string = str_replace('"', '', $string); | |
// anything not in this list gets dropped | |
$unreserved_charset = '/[^a-zA-Z0-9_\-\.~]/'; | |
$result = preg_replace($unreserved_charset,'',$string); | |
return $result; | |
} | |
} | |
/** | |
* Add this function to your Application/Module.php file | |
* | |
* */ | |
/** | |
* define my custom viewHelpers/escape functions etc here. | |
* @return array | |
*/ | |
public function getViewHelperConfig() { | |
return array( | |
'invokables' => array( | |
'Urlize' => 'Application\View\Helper\Urlize', | |
) | |
); | |
} | |
/*********************************** | |
* Example Usage in view.phtml | |
* | |
* <a href="<?php echo $this->url('album', | |
* array('action' => $this->Urlize($album->title) , | |
* 'id' => $album->id));?>" class="btn btn-primary">Details</a> | |
************************************/ | |
?> |
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
<a href="<?php echo $this->url('album', array('action' => $this->Urlize($album->title) , 'id' => $album->id));?>" class="btn btn-primary">Details</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment