define('UPLOAD_IMG_PATH','./wordpress/'); //$str = 'sdasda <img src="http://news.xinhuanet.com/forum/2012-07/12/123399677_31n.jpgsdasd" />'; function lx_findPicAndDownload($str){ preg_match_all('/http://([^/]+/)+[^.]+.(jpg|png|gif)/i',$str, $out); for($i=0;$i<count($out[0]);$i++){ $new_file = lx_downloadAndSaveFile($out[0][$i],$out[2][$i]); if($new_file){ $str = str_replace($out[0][$i],$new_file,$str); } } return $str; } function lx_downloadAndSaveFile($name, $ext, $tried_count=0){ if($tried_count>2) return; $file = file_get_contents($name); if($file){ $path = '/wp-content/uploads/'.date('Y/m').'/'.lx_randFileName($ext); file_put_contents(UPLOAD_IMG_PATH.$path, $file); //file_put_contents('/tmp/log1.txt',UPLOAD_IMG_PATH.$path); return $path; }else{ sleep(3); return lx_downloadAndSaveFile($name, $ext, ++$tried_count); } } function lx_randFileName($ext){ return date('YmdHis').floor(rand(10000,99999)).'.'.substr(trim($ext),-3); }