Skip to content

Instantly share code, notes, and snippets.

@matdave
Created April 2, 2021 17:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matdave/eb79d856dddfc2d36a7c130c5e9a0a2f to your computer and use it in GitHub Desktop.
Save matdave/eb79d856dddfc2d36a7c130c5e9a0a2f to your computer and use it in GitHub Desktop.
Fix Non UTF Characters in a MODX Resource
<?php
$regex = <<<'END'
/
(
(?: [\x00-\x7F] # single-byte sequences 0xxxxxxx
| [\xC0-\xDF][\x80-\xBF] # double-byte sequences 110xxxxx 10xxxxxx
| [\xE0-\xEF][\x80-\xBF]{2} # triple-byte sequences 1110xxxx 10xxxxxx * 2
| [\xF0-\xF7][\x80-\xBF]{3} # quadruple-byte sequence 11110xxx 10xxxxxx * 3
){1,100} # ...one or more times
)
| . # anything else
/x
END;
$resource = $modx->getObject('modResource', 3);
if(!empty($resource)){
$content = preg_replace($regex, '$1', $resource->get('content'));
$resource->set('content', $content);
if($resource->save()){
echo "Resource Saved!";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment