我採集了幾十萬文章,然後發現這些文章全都沒有縮略圖,於是乎,非常的醜,醜到爆炸!!
於是乎,動動小手,寫一個隨機縮略圖的代碼,將所有的文章都給設置一張縮略圖!
代碼
🔒 隱藏內容:會員可查看
<?php
$biao = "phome_";//表前綴
$fbiao = "news";//什麼表
require('e/class/connect.php'); //引入數據庫配置文件和公共函數文件
require('e/class/db_sql.php'); //引入數據庫操作文件
$link=db_connect(); //連接MYSQL
$empire=new mysqlquery(); //聲明數據庫操作類
// 檢查是否第一次執行腳本
if (!isset($_COOKIE['mycookie'])) {
setcookie('mycookie', '1', time() + 86400, '/');
$empire->query("UPDATE `{$biao}ecms_{$fbiao}` SET `titlepic` = ''");
}
$sql=$empire->query("SELECT id FROM `{$biao}ecms_{$fbiao}` WHERE `titlepic` = ' ' ORDER BY `id` ASClimit 100");
while($r=$empire->fetch($sql)) //循環獲取查詢記錄
{
$r2 = json_decode(file_get_contents("http://api.4s5.cn/Img/api.php"),true);
if($r2['code']==200){
$empire->query("UPDATE `{$biao}ecms_{$fbiao}` SET `titlepic` = '{$r2['img']}' WHERE `id` = {$r['id']}");
echo "成功".$r['id']."》》{$r2['img']}<br>";
}else{
echo "失敗".$r['id']."》》<br>".print_r($r2);
}
}
echo '<meta http-equiv="refresh" content="0"/>';
db_close(); //關閉MYSQL鏈接
$empire=null; //注消操作類變量
?>第二個需求
文章雖然有縮略圖了,但是!
文章裏面採集的圖片,別網站都開了防盜鏈,在我這網站根本顯示不了!
你說手動吧,幾十萬,你說自動吧,火車頭重新採集?太麻煩了!
於是乎,我再動動小手!
寫成一個自動將文章裏面圖片給隨機替換的代碼!
原理匹配文章裏面的圖片鏈接,然後將鏈接替換爲指定的圖片鏈接!
這樣就可以解決文章裏面出現各類的圖片不顯示!
🔒 隱藏內容:會員可查看
<?php
$biao = "phome_";//表前綴
$fbiao = "news";//什麼表
require('e/class/connect.php'); //引入數據庫配置文件和公共函數文件
require('e/class/db_sql.php'); //引入數據庫操作文件
$link=db_connect(); //連接MYSQL
$empire=new mysqlquery(); //聲明數據庫操作類
$id = empty($_GET['next'])?0:$_GET['next'];
$sql=$empire->query("SELECT id,stb FROM `{$biao}ecms_{$fbiao}` WHERE `id` >= {$id} ORDER BY `id` ASC limit 100"); //查詢新聞表最新10條記錄
while($r=$empire->fetch($sql)) //循環獲取查詢記錄
{
$img = json_decode(file_get_contents("http://api.4s5.cn/Img/api.php?num=50"),true);
if($img['code']==200){
$r2 = $empire->fetch1("SELECT id,newstext FROM `{$biao}ecms_{$fbiao}_data_{$r['stb']}` WHERE `id` = {$r['id']} ");
$newstext = randomReplaceImageLinks(stripslashes($r2['newstext']),$img['img'],0);
$empire->query("UPDATE `{$biao}ecms_{$fbiao}_data_{$r['stb']}` SET `newstext` = '".addslashes($newstext)."' WHERE `id` = {$r['id']}");
echo "成功".$r['id']."》》<br>";
}else{
echo "失敗".$r['id']."》》<br>";
}
$id=$r['id'];
}
echo '<meta http-equiv="refresh" content="1;url=http://'.$_SERVER['HTTP_HOST'].''.$_SERVER['SCRIPT_NAME'].'?next='.$id.'">';
db_close(); //關閉MYSQL鏈接
$empire=null; //注消操作類變量
//type 0爲替換img全部,1爲只替換鏈接,默認替換鏈接
function randomReplaceImageLinks($content, $imageLinks, $type = 1) {
// 匹配 img 標籤中的 src 屬性
$pattern = '/<img.+?src=[\'"](?P<src>.+?)[\'"].*?>/i';
// 獲取所有匹配到的圖片鏈接
preg_match_all($pattern, $content, $matches);
if (empty($matches[$type])) {
return $content;
}
// 遍歷所有匹配到的圖片鏈接,用隨機選擇的圖片鏈接替換
foreach ($matches[$type] as $oldImageLink) {
// 隨機選擇一個圖片鏈接
$randomIndex = array_rand($imageLinks);
$newImageLink = $imageLinks[$randomIndex];
$newImageLink = $type==0?'<img src="' . $newImageLink . '">':$newImageLink;
$content = str_replace($oldImageLink, $newImageLink, $content);
}
return $content;
}代碼如何使用呢?
其實很簡單!
在帝國根目錄建立一個1.php,代碼放進去,然後瀏覽器訪問這個就行!
不會用怎麼辦?呵呵o(* ̄︶ ̄*)o,付費幫您咯!