【PHP】XMLをパースする際のエラーを極限まで吸収したい。

ということで独自関数のメモ。

function simplexmlLoadString($str,$class_name = "SimpleXMLElement",$options = 0){
	$search = array("\0", "\x01", "\x02", "\x03", "\x04", "\x05", "\x06"
		, "\x07", "\x08", "\x0b", "\x0c", "\x0e", "\x0f");
	$str = str_replace($search, '', $str);
	$str = preg_replace('/[\x00-\x08]/','',$str);
	$str = preg_replace('/[\x0B-\x0C]/','',$str);
	$str = preg_replace('/[\x0E-\x1F]/','',$str);
	$str = mb_convert_encoding($str,'eucjp-win','UTF-8');
	$str = mb_convert_encoding($str,'SJIS-win','eucjp-win');
	$str = mb_convert_encoding($str,'UTF-8','SJIS-win');
	$data = simplexml_load_string($str,$class_name,$options);
	return $data;
}