$encode = mb_convert_encoding($val,"UTF-8","Shift_JIS");
$url_encode =urlencode($encode);
$html="http://thesaurus.weblio.jp/content/$url_encode";;
$html2=file_get_contents($html);
は
$html=file_get_contents("http://thesaurus.weblio.jp/content/";.urlencode(mb_convert_encoding($val,"UTF-8","Shift_JIS")));とか省略してしまえば?他にもあるけど。