Created
February 14, 2014 14:18
-
-
Save bkmorse/9001727 to your computer and use it in GitHub Desktop.
twitter has bad characters in twitter messages that will make your MySQL fail, so this cleans it up for an insert.
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 | |
| //Test string that will blow up MySQL insert: UlQgQFJabm9ydGg6IEhhcHB5IEJpcnRoZGF5IEBkdWNpZG5pIHlvdXIgbXVzaWMgcmVtaW5kcyBtZSBvZiBteSBiZXN0IG1lbW9yaWVzICNORlNNb3ZpZSAjc2F0ZWxsaXRlZmxpZ2h0IPCfjrbwn5KZ8J+agA== | |
| if ( ! defined('BASEPATH')) exit('No direct script access allowed'); | |
| function remove_non_utf8($string) { | |
| //reject overly long 2 byte sequences, as well as characters above U+10000 and replace with ? | |
| $string = preg_replace('/[\x00-\x08\x10\x0B\x0C\x0E-\x19\x7F]'. | |
| '|[\x00-\x7F][\x80-\xBF]+'. | |
| '|([\xC0\xC1]|[\xF0-\xFF])[\x80-\xBF]*'. | |
| '|[\xC2-\xDF]((?![\x80-\xBF])|[\x80-\xBF]{2,})'. | |
| '|[\xE0-\xEF](([\x80-\xBF](?![\x80-\xBF]))|(?![\x80-\xBF]{2})|[\x80-\xBF]{3,})/S', | |
| '', $string ); | |
| //reject overly long 3 byte sequences and UTF-16 surrogates and replace with ? | |
| $string = preg_replace('/\xE0[\x80-\x9F][\x80-\xBF]'. | |
| '|\xED[\xA0-\xBF][\x80-\xBF]/S','', $string ); | |
| return $string; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment