Created
April 2, 2021 17:28
-
-
Save matdave/eb79d856dddfc2d36a7c130c5e9a0a2f to your computer and use it in GitHub Desktop.
Fix Non UTF Characters in a MODX Resource
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
<?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