简单设计思路就是先检测是否带有图片,然后把图片保留下来,用其他的占位符替换原内容的图片位置,伪原创后,再替换回来。
class wyc{ /** * 伪原创 */ public function index($val){ $data = $val; //进行检查是否有图片 $preg = '/<img([^>]+)>/';//正则 $pics = [];//存储图片 $replace = [];//存储占位符 $abc = 'abcdefghijklmnopqrstuvwsyz';//字母串-26张图片内 $res = preg_match_all($preg,$data,$match); if($res){ $pics = $match[0]; foreach($pics as $k=>$v){ $replace[$k] = '['.$abc[$k].']'; } $data = str_ireplace($pics,$replace,$data); } $info = $this->fy($data); if($res){ $info = str_ireplace($replace,$pics,$info); } return $info; } function translate($text,$from,$to){ $url = "http://translate.google.cn/translate_a/single?client=gtx&dt=t&ie=UTF-8&oe=UTF-8&sl=$from&tl=$to&q=". urlencode($text); set_time_limit(0); $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); curl_setopt($ch, CURLOPT_HEADER, false); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS,20); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 40); curl_setopt($ch, CURLOPT_URL, $url); $result = curl_exec($ch); curl_close($ch); $result = json_decode($result); if(!empty($result)){ foreach($result[0] as $k){ $v[] = $k[0]; } return implode(" ", $v); } } function fy($data){ $zh_en=$this->translate($data,'zh-CN','EN'); if($zh_en){ $en_zh=$this->translate($zh_en,'EN','zh-CN'); if($en_zh){ $info=$en_zh; return $info; }else{ $this->error(); } }else{ $this->error(); } } function error(){ echo '翻译内容太多或者操作太频繁!';exit; } }
版权声明:本文发布于与老涂一起写代码 内容均来源于互联网 如有侵权联系删除
快来评论,快来抢沙发吧~