火车头伪原创插件PHP版,如何在标题前面插入关键词? 悬赏1元 已结束
<p>大家好,我想在我的插件里面改个东西,能不能在标题前面插入我要的关键词?</p><p><?php
</p><p>
</p><p>set_time_limit(270);
</p><p>error_reporting(E_ERROR | E_WARNING | E_PARSE);
</p><p>define('TITLE_SEPAR', 'xxxxxx');
</p><p>define('TITLE_SEPAR2', '262661');
</p><p>$url = '这里填写你的小发猫伪原创API地址';
</p><p>$content_tag_name = '内容';
</p><p>$headdd = '';
</p><p>$taill = '';
</p><p>switch($LabelArray['PageType'])
</p><p>{
</p><p> case 'List'://处理列表页,只能处理html
</p><p> break;
</p><p> case 'Pages'://处理多页,只能处理html
</p><p> break;
</p><p> case 'Content'://处理默认页,只能处理html
</p><p> break;
</p><p> case 'Save'://只有保存时是可以处理标签值的
</p><p> // 保存原文
</p><p> try {
</p><p> /**********************************************************************/
</p><p> // 这一步用来获取伪原创文章
</p><p> /**********************************************************************/
</p><p> $title = $LabelArray['标题'];
</p><p> $title = '标题:'.$title;
</p><p> $content = $LabelArray[$content_tag_name];
</p><p> $article_src = compose_article($title, $content);
</p><p> $article_src_b = $article_src;
</p><p> $article_new = get_wyc_article($article_src);
</p><p> $title_wyc = trim($article_new[0]);
</p><p> $content_wyc = trim($article_new[1]);
</p><p> $content_wyc = fix_newline($content_wyc);
</p><p> $LabelArray[$content_tag_name] = $headdd. $content_wyc. $taill;
</p><p> $title_wyc = str_replace(array('。',',','%'), array(' ',' ',' '), $title_wyc);
</p><p> $LabelArray['标题'] = $title_wyc;
</p><p> }
</p><p> catch (Exception $e) {
</p><p> $LabelArray['标题'] .= $e->getMessage();
</p><p> $LabelArray[$content_tag_name] .= $e->getMessage();
</p><p> }
</p><p> break;
</p><p> default:
</p><p> //$LabelArray[$content_tag_name]=curl_request($url, array('wenzhang'=>$LabelArray[$content_tag_name] ));
</p><p>}
</p><p>echo serialize($LabelArray);
</p><p>function compose_article($title, $content) {
</p><p> $separator = compose_separator();
</p><p> return $title.$separator.$content;
</p><p>}
</p><p>function compose_separator() {
</p><p> return PHP_EOL.'('.TITLE_SEPAR2.')'.PHP_EOL;
</p><p>}
</p><p>function fix_separator($article) {
</p><p> return $article;
</p><p>}
</p><p>function get_wyc_article($str) {
</p><p> global $url;
</p><p> $separator = compose_separator();
</p><p> $separator = str_replace(PHP_EOL, '', $separator);
</p><p> $wyc = curl_request($url, array('wenzhang'=>$str));
</p><p> $wyc_f = $wyc;
</p><p> $wyc = fix_separator($wyc);
</p><p> $wyc = explode($separator, $wyc);
</p><p> if (isset($wyc[0])){
</p><p> $wyc[0] = trim($wyc[0]);
</p><p> $wyc[0] = ltrim($wyc[0], '标题');
</p><p> $wyc[0] = ltrim($wyc[0], '目:');
</p><p> $wyc[0] = ltrim($wyc[0], ':');
</p><p> $wyc[0] = ltrim($wyc[0], ':');
</p><p> }
</p><p> //if (isset($wyc[1])) $wyc[1] = trim($wyc[1]);
</p><p> //$wyc[1] = $wyc_f.'jjjjjjjj'.$wyc[1];
</p><p> return $wyc;
</p><p>}
</p><p>function get_wyc_title($str) {
</p><p> $title = get_wyc_article($str.PHP_EOL.PHP_EOL.PHP_EOL.$str.PHP_EOL.PHP_EOL.PHP_EOL.$str);
</p><p> $title = fix_newline($title);
</p><p> $title = explode(PHP_EOL, $title);
</p><p> return $title[0];
</p><p>}
</p><p>function get_keywords($title, $contents) {
</p><p> $url_kw = 'xx';
</p><p> $kws = curl_request($url_kw, array(
</p><p> 'title'=>$title,
</p><p> 'len'=>100,
</p><p> 'text'=>$contents));
</p><p> return $kws;
</p><p>}
</p><p>function remove_alt($contents) {
</p><p> $contents = preg_replace('/alt=\"(.)\"/', '', $contents);
</p><p> return $contents;
</p><p>}
</p><p>function fix_title($contents) {
</p><p> $punctuation_symbol = array('。', '?', ',', ':', ';', '、', '!',
</p><p> '.', '?', ',', ':', ';', '!');
</p><p> $contents = str_replace($punctuation_symbol, '', $contents);
</p><p> return $contents;
</p><p>}
</p><p>function br2newline($contents) {
</p><p> $contents = str_replace('<br>', PHP_EOL, $contents);
</p><p> $contents = str_replace('<br/>', PHP_EOL, $contents);
</p><p> $contents = str_replace('<br />', PHP_EOL, $contents);
</p><p> $contents = str_replace('<BR/>', PHP_EOL, $contents);
</p><p> $contents = str_replace('<BR>', PHP_EOL, $contents);
</p><p> $contents = str_replace('<BR />', PHP_EOL, $contents);
</p><p> return $contents;
</p><p>}
</p><p>function newline2br($contnets) {
</p><p> $contnets = str_replace(PHP_EOL, "<br>", $contnets);
</p><p>// $contnets = str_replace('><br><', '><', $contnets);
</p><p> $contnets = str_replace('<p><br>', '<p>', $contnets);
</p><p> return $contnets;
</p><p>}
</p><p>function delete_newline($contents) {
</p><p> $contents = fix_newline($contents);
</p><p>// $contents = str_replace(PHP_EOL.PHP_EOL, PHP_EOL, $contents);
</p><p>// $contents = str_replace('>'.PHP_EOL, '>', $contents);
</p><p> return $contents;
</p><p>}
</p><p>function reset_newline_win($contents) {
</p><p> // 优化换行符
</p><p> $contents = str_replace("\r\n", "\n", $contents);
</p><p> $contents = str_replace("\r", "\n", $contents);
</p><p> $contents = str_replace("\n", PHP_EOL, $contents);
</p><p> return $contents;
</p><p>}
</p><p>function fix_newline($data) {
</p><p> $data = str_replace("\r", "\n", $data);
</p><p> while(strpos($data, "\n\n") !== false) {
</p><p> $data = str_replace("\n\n", "\n", $data);
</p><p> }
</p><p> $data = str_replace("\n", PHP_EOL, $data);
</p><p> return $data;
</p><p>}
</p><p>function clean_contents($contents) {
</p><p>// $str = preg_replace('#<([^>\s/]+)[^>]>#','<$1>', $contents);
</p><p>// return $str;
</p><p> $sa = new cleanHtml;
</p><p> $sa->allow = array( 'src' );
</p><p> $sa->exceptions = array(
</p><p> 'img' => array( 'src', 'alt' ),
</p><p> //'a' => array( 'href', 'title' ),
</p><p> 'iframe'=>array('src','frameborder'),
</p><p> );
</p><p> $str = $sa->strip( $contents );
</p><p> return $str;
</p><p>}
</p><p>function xfm_strong_str_replace_once($search, $replace, $subject) {
</p><p> $firstChar = strpos($subject, $search);
</p><p> if($firstChar !== false) {
</p><p> $beforeStr = substr($subject,0,$firstChar);
</p><p> $afterStr = substr($subject, $firstChar + strlen($search));
</p><p> return $beforeStr.$replace.$afterStr;
</p><p> } else {
</p><p> return $subject;
</p><p> }
</p><p>}
</p><p>//参数1:访问的URL,参数2:post数据(不填则为GET),参数3:提交的$cookies,参数4:是否返回$cookies
</p><p>function curl_request($url,$post='',$cookie='', $returnCookie=0){
</p><p> if (! extension_loaded('curl')) {
</p><p> file_exists('./ext/php_curl.dll') && dl('php_curl.dll'); // 加载扩展
</p><p> }
</p><p>
</p><p> $curl = curl_init();
</p><p> curl_setopt($curl, CURLOPT_URL, $url);
</p><p> curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; Trident/6.0)');
</p><p> if (ini_get('open_basedir') == '' && strtolower(ini_get('safe_mode')) != 'on'){
</p><p> curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
</p><p> }
</p><p> curl_setopt($curl, CURLOPT_AUTOREFERER, 1);
</p><p> curl_setopt($curl, CURLOPT_REFERER, "http://XXX");
</p><p> if($post) {
</p><p> curl_setopt($curl, CURLOPT_POST, 1);
</p><p> curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($post));
</p><p> }
</p><p> if($cookie) {
</p><p> curl_setopt($curl, CURLOPT_COOKIE, $cookie);
</p><p> }
</p><p> curl_setopt($curl, CURLOPT_HEADER, $returnCookie);
</p><p> curl_setopt($curl, CURLOPT_TIMEOUT, 150);
</p><p> curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
</p><p> $data = curl_exec($curl);
</p><p> if (curl_errno($curl)) {
</p><p> return curl_error($curl);
</p><p> }
</p><p> curl_close($curl);
</p><p> if($returnCookie){
</p><p> list($header, $body) = explode("\r\n\r\n", $data, 2);
</p><p> preg_match_all("/Set-Cookie:([^;]);/", $header, $matches);
</p><p> $info['cookie'] = substr($matches[1][0], 1);
</p><p> $info['content'] = $body;
</p><p> return $info;
</p><p> }else{
</p><p> return $data;
</p><p> }
</p><p>}
</p><p>//echo $tag;
</p><p>// 计算中文字符串长度
</p><p>function utf8_strlen($string = null) {
</p><p>// 将字符串分解为单元
</p><p>preg_match_all("/./us", $string, $match);
</p><p>// 返回单元个数
</p><p>return count($match[0]);
</p><p>}
</p><p>function reg_escape( $str )
</p><p>{
</p><p> $conversions = array( "^" => "\^", "[" => "[", "." => ".", "$" => "\$", "{" => "{", "" => "*", "(" => "(", "\" => "\\", "/" => "\/", "+" => "+", ")" => ")", "|" => "\|", "?" => "\?", "<" => "\<", ">" => ">" );
</p><p> return strtr( $str, $conversions );
</p><p>}
</p><p>
</p><p>/
</p><p> Strip attribute Class
</p><p> Remove attributes from XML elements
</p><p> @author David (semlabs.co.uk)
</p><p> @version 0.2.1
</p><p>/
</p><p>
</p><p>class cleanHtml{
</p><p>
</p><p> public $str = '';
</p><p> public $allow = array();
</p><p> public $exceptions = array();
</p><p> public $ignore = array();
</p><p>
</p><p> public function strip( $str )
</p><p> {
</p><p> $this->str = $str;
</p><p>
</p><p> if( is_string( $str ) && strlen( $str ) > 0 )
</p><p> {
</p><p> $res = $this->findElements();
</p><p> if( is_string( $res ) )
</p><p> return $res;
</p><p> $nodes = $this->findAttributes( $res );
</p><p> $this->removeAttributes( $nodes );
</p><p> }
</p><p>
</p><p> return $this->str;
</p><p> }
</p><p>
</p><p> private function findElements()
</p><p> {
</p><p> # Create an array of elements with attributes
</p><p> $nodes = array();
</p><p> preg_match_all( "/<([^ !\/>\n]+)([^>])>/i", $this->str, $elements );
</p><p> foreach( $elements[1] as $el_key => $element )
</p><p> {
</p><p> if( $elements[2][$el_key] )
</p><p> {
</p><p> $literal = $elements[0][$el_key];
</p><p> $element_name = $elements[1][$el_key];
</p><p> $attributes = $elements[2][$el_key];
</p><p> if( is_array( $this->ignore ) && !in_array( $element_name, $this->ignore ) )
</p><p> $nodes[] = array( 'literal' => $literal, 'name' => $element_name, 'attributes' => $attributes );
</p><p> }
</p><p> }
</p><p>
</p><p> # Return the XML if there were no attributes to remove
</p><p> if( !$nodes[0] )
</p><p> return $this->str;
</p><p> else
</p><p> return $nodes;
</p><p> }
</p><p>
</p><p> private function findAttributes( $nodes )
</p><p> {
</p><p>
</p><p> # Extract attributes
</p><p> foreach( $nodes as &$node )
</p><p> {
</p><p> preg_match_all( "/([^ =]+)\s=\s[\"|']{0,1}([^\"']*)[\"|']{0,1}/i", $node['attributes'], $attributes );
</p><p> if( $attributes[1] )
</p><p> {
</p><p> foreach( $attributes[1] as $att_key => $att )
</p><p> {
</p><p> $literal = $attributes[0][$att_key];
</p><p> $attribute_name = $attributes[1][$att_key];
</p><p> $value = $attributes[2][$att_key];
</p><p> $atts[] = array( 'literal' => $literal, 'name' => $attribute_name, 'value' => $value );
</p><p> }
</p><p> }
</p><p> else
</p><p> $node['attributes'] = null;
</p><p>
</p><p> $node['attributes'] = $atts;
</p><p> unset( $atts );
</p><p> }
</p><p>
</p><p> return $nodes;
</p><p> }
</p><p>
</p><p> private function removeAttributes( $nodes )
</p><p> {
</p><p>
</p><p> # Remove unwanted attributes
</p><p> foreach( $nodes as $node )
</p><p> {
</p><p>
</p><p> # Check if node has any attributes to be kept
</p><p> $node_name = $node['name'];
</p><p> $new_attributes = '';
</p><p> if( is_array( $node['attributes'] ) )
</p><p> {
</p><p> foreach( $node['attributes'] as $attribute )
</p><p> {
</p><p> if( ( is_array( $this->allow ) && in_array( $attribute['name'], $this->allow ) ) || $this->isException( $node_name, $attribute['name'], $this->exceptions ) )
</p><p> $new_attributes = $this->createAttributes( $new_attributes, $attribute['name'], $attribute['value'] );
</p><p> }
</p><p> }
</p><p> $replacement = ( $new_attributes ) ? "<$node_name $new_attributes>" : "<$node_name>";
</p><p> $this->str = preg_replace( '/'. reg_escape( $node['literal'] ) .'/', $replacement, $this->str );
</p><p> }
</p><p>
</p><p> }
</p><p>
</p><p> private function isException( $element_name, $attribute_name, $exceptions )
</p><p> {
</p><p> if( array_key_exists($element_name, $this->exceptions) )
</p><p> {
</p><p> if( in_array( $attribute_name, $this->exceptions[$element_name] ) )
</p><p> return true;
</p><p> }
</p><p>
</p><p> return false;
</p><p> }
</p><p>
</p><p> private function createAttributes( $new_attributes, $name, $value )
</p><p> {
</p><p> if( $new_attributes )
</p><p> $new_attributes .= " ";
</p><p> $new_attributes .= "$name=\"$value\"";
</p><p>
</p><p> return $new_attributes;
</p><p> }
</p><p>
</p><p>}
</p><p></p><p>?>
</p>