Created
February 7, 2021 01:48
-
-
Save nachoaguirre/671cb37eac535a51d26229935b18b495 to your computer and use it in GitHub Desktop.
Texto a Slug
This file contains hidden or 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
function toSlug(str) { | |
str = str.replace(/^\s+|\s+$/g, ''); | |
str = str.toLowerCase(); | |
var from = "ãàáäâẽèéëêìíïîõòóöôùúüûñç·/_,:;"; | |
var to = "aaaaaeeeeeiiiiooooouuuunc------"; | |
for (var i=0, l=from.length ; i<l ; i++) { str = str.replace(new RegExp(from.charAt(i), 'g'), to.charAt(i)); } | |
str = str.replace(/[^a-z0-9 -]/g, '').replace(/\s+/g, '-').replace(/-+/g, '-'); return str; | |
} | |
// Itera sobre objeto para agregar propiedad 'slug', basandose en propiedad 'nombre' | |
objeto.forEach(function(item) { | |
if(item.nombre !== null){ item.slug = toSlug(item.nombre); } | |
else { item.slug = null; } | |
}); |
This file contains hidden or 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
function toSlug($string) { | |
$slug = \Transliterator::createFromRules(':: Any-Latin;' . ':: NFD;' . ':: [:Nonspacing Mark:] Remove;' . ':: NFC;' . ':: [:Punctuation:] Remove;' . ':: Lower();' . '[:Separator:] > \'-\'')->transliterate( $string ); | |
return $slug; | |
} | |
// Itera sobre objeto para agregar propiedad 'slug', basandose en propiedad 'nombre' | |
foreach($objeto as $k => $v) { if($v->nombre !== null){ $v->slug = toSlug($v->nombre); } else { $v->slug = null; } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment