yj-t
12/30/2019 - 8:06 AM

MODX output modifier to clean classnames

<?php
$input = $modx->getOption('input', $scriptProperties, '', true);
$output = $modx->getOption('options', $scriptProperties, '', true);

$classnames = explode(' ', $input); 
$results = [];
foreach ($classnames as $token) {
    // Strip out any % encoded octets
    $normalized = preg_replace('|%[a-fA-F0-9][a-fA-F0-9]|', '', $token);
    // Replace _ with - and lowercase for old times' sake
    $normalized = str_replace('_', '-', strtolower($normalized));
    // Limit to a-z,0-9,-
    $normalized = preg_replace('/[^a-z0-9-]/', '', $normalized); 
    // Remove numeric starting characters
    $normalized = ltrim($normalized, '0123456789-');
    
    if (empty($normalized)) continue;
    $results[] = $normalized;
}

if (!empty($results)) $output = implode(' ', $results);
return $output;