I collected hundreds of thousands of articles, only to discover that none of them had thumbnails – which made the whole thing look absolutely hideous!!
So, just use your little hand to write some code that generates random thumbnails – and assign a thumbnail to every article!
code
隐藏内容:会员可查看
<?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; //注消操作类变量
?>
Second requirement
Although the article includes an thumbnail, however!
The images collected in this article have防盗链设置 on other websites; as a result, they simply won't display on my website!
If we go with manual processing – that would cost hundreds of thousands – or with automatic processing – would that require the locomotive to collect data all over again? That would be too much hassle!
So, I'll give my little hands another try!
Write a piece of code that randomly replaces images within an article!
Match the image links in the article and then replace them with the specified image links!
This solves the issue where various images within an article fail to display!
隐藏内容:会员可查看
<?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;
}
How do I use the code?
It's actually quite simple!
Create a file named 1.php in the root directory of your server, enter the code inside it, and then simply open this file in your web browser!
Don't know how to use it? Hehe! (* ̄︶ ̄*) – I'll help you for a fee!