上传txt文章,自动转变为英文文章,插入图片地址,自动识别并插入html

新手上路篇

上传txt文件,中文自动转化为英文,生成html

网页关键词自动抽取本地文件

描述自动截取前一段文章

网页自动获取目录的十篇文章作文相关文章

废话不多说,直接冲:

先建立文件夹articles,用来储存生成后的html

建立关键词文件keywords.txt,一行一个关键词

处理文件代码:


<?php

require 'vendor/autoload.php';

use Stichoza\GoogleTranslate\GoogleTranslate;

// 获取随机相关文章
function getRandomRelatedArticles($count = 10) {
    $allFiles = glob('articles/*.html');
    shuffle($allFiles);
    $relatedFiles = array_slice($allFiles, 0, $count);

    return $relatedFiles;
}

// 从keywords.txt文件中获取元关键词
function getMetaKeywords() {
    $keywordsFile = 'keywords.txt';

    if (file_exists($keywordsFile)) {
        $keywordsArray = file($keywordsFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // 获取文件的每一行为数组元素
        $keywordsArray = array_slice($keywordsArray, 0, 10); // 获取前10个关键词
        $keywords = implode(',', $keywordsArray); // 将关键词数组连接回字符串,用逗号分隔
        return $keywords;
    }

    return ''; // 如果文件不存在,返回空字符串
}


// 从内容中获取元描述
function getMetaDescription($content) {
    // 使用正则表达式匹配第一段
    $matches = [];
    preg_match('/<p>(.*?)<\/p>/s', $content, $matches);

    // 如果找到匹配,返回第一个匹配项,否则返回空字符串
    return $matches[1] ?? '';
}

if (isset($_FILES['articles'])) {
    $articles = $_FILES['articles'];
    $translator = new GoogleTranslate();
    $translator->setTarget('en');

    for ($i = 0; $i < count($articles['name']); $i++) {
        if ($articles['error'][$i] === UPLOAD_ERR_OK) {
            $content = file_get_contents($articles['tmp_name'][$i]);
            $title = pathinfo($articles['name'][$i], PATHINFO_FILENAME);

            $translatedTitle = $translator->translate($title);
            $translatedContent = $translator->translate($content);

            $metaDescription = getMetaDescription($translatedContent);
            $metaKeywords = getMetaKeywords();

if (isset($_FILES['articles'])) {
    $articles = $_FILES['articles'];

    // 创建 GoogleTranslate 的新实例
    $translator = new GoogleTranslate();

    // 将目标语言设置为英语
    $translator->setTarget('en');

    // 遍历每个上传的文件
    for ($i = 0; $i < count($articles['name']); $i++) {
        if ($articles['error'][$i] === UPLOAD_ERR_OK) {
            $content = file_get_contents($articles['tmp_name'][$i]);
            $title = pathinfo($articles['name'][$i], PATHINFO_FILENAME);

            // Translate the Chinese title to English
            $translatedTitle = $translator->translate($title);

            // Translate the Chinese content to English
            $translatedContent = $translator->translate($content);

            // Find and insert image links
    $contentWithImages = preg_replace_callback('/(<img src=[\'"])([^\'"]+)([\'"])/i', function ($matches) {
    return $matches[1] . $matches[2] . $matches[3] . ' style="max-width: 100%; height: auto;">';
}, $translatedContent);

$formattedContent = nl2br($contentWithImages);


            $relatedArticles = getRandomRelatedArticles();
            $relatedArticlesHtml = '';
            foreach ($relatedArticles as $article) {
               $relatedArticlesHtml .= '<li><a href="' . str_replace('articles/', '', $article) . '">' . basename($article, '.html') . '</a></li>';

            }
        }
    }
}

            $html = '<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="' . $metaDescription . '">
<meta name="keywords" content="' . $metaKeywords . '">


<link rel="shortcut icon" href="favicon.ico" type="image/x-icon">
<link rel="icon" href="favicon.ico" type="image/x-icon">
    <title>' . $translatedTitle . '</title>
     <script charset="UTF-8" id="LA_COLLECT" src="//sdk.51.la/js-sdk-pro.min.js"></script>
<script>LA.init({id:"K2vcKRl2WRHQQS1K",ck:"K2vcKRl2WRHQQS1K"})</script>
        <!-- Google tag (gtag.js) -->
<script async src="https://www.googletagmanager.com/gtag/js?id=G-Y2N21X5N2W"></script>
<script>
  window.dataLayer = window.dataLayer || [];
  function gtag(){dataLayer.push(arguments);}
  gtag("js", new Date());

  gtag("config", "G-Y2N21X5N2W");
</script>
<link rel="stylesheet" href="style.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.2/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css">
    <style>
        body {
           font-family: "Microsoft YaHei", "微软雅黑", "宋体", "SimSun", "Roboto", Arial, sans-serif;
            margin: 0;
            padding: 0;
            background-color: #F44336;
            color: #edde28;
            line-height: 1.6;
        }


        .container {
            max-width: 800px;
            margin: 0 auto;
            padding: 20px;
        }

        h1 {
            font-size: 40px;
            text-align: center;
            margin-top: 50px;
            margin-bottom: 20px;
        }
       h2 a, ul a {
    color: #edde28; 
    text-decoration: none; /* 移除下划线 */
}

h2 a:hover, ul a:hover {
    color: #FFD700; 
}

        .logo {
            display: block;
            width: 200px;
            margin: 20px auto;
        }

        .article-content {
            margin: 20px;
        }

        .button {
            display: inline-block;
            margin: 20px 10px;
            padding: 10px 20px;
            background-color: #B8860B;
            color: #F44336;
            font-size: 18px;
            text-align: center;
            text-decoration: none;
            border-radius: 4px;
            transition: background-color 0.3s ease;
        }

        .button:hover {
            background-color: #F44336;
            color: #B8860B;
        }

 ul li {
        word-wrap: break-word;
    }


    </style>
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
    <!-- Add the statistics scripts and other head elements here -->
</head>
<body>
    <div class="container">
        <img src="/logo.png" alt="Logo" class="logo">
        <h1>' . $translatedTitle . '</h1>
        <div class="article-content">' . $formattedContent . '</div>
        <div class="related-articles-container">
       <h2>related articles</h2>
    <ul>
        ' . $relatedArticlesHtml . '
    </ul>
        <a href="javascript:history.back()" class="button">Back</a>
        <a href="https://www.guibaofang.com" class="button">Home</a>
    </div>
    <!-- Add the statistics scripts and other body elements here -->
      <footer>
            <div class="container">
                <div class="row">
                    <div class="col-md-6">
<h1>
    <i class="fas fa-question-circle" aria-hidden="true"></i> Only this? Contact us to see more!
</h1>

<p>
    <i class="fas fa-map-marker-alt" aria-hidden="true"></i> Address: Hometown of Chinese Jade Ware - China Sihui
</p>
<p>
    <a href="https://api.whatsapp.com/send?phone=601160969184&text=Hello!%20I%20am%20interested%20in%20your%20Nanhong%20agate%20jewelry.%20Can%20you%20please%20provide%20more%20information%3F" target="_blank" class="whatsapp-link">
        <i class="fab fa-whatsapp" aria-hidden="true"></i> Contact us on WhatsApp
    </a>
</p>

<p>
    <a href="wechat.jpg" target="_blank" class="wechat-link">
        <i class="fab fa-weixin" aria-hidden="true"></i> WeChat:@guibaofang
    </a>
</p>

<p>
    <a href="https://www.youtube.com/@guibaofang" target="_blank" class="youtube-link">
        <i class="fab fa-youtube" aria-hidden="true"></i> YouTube
    </a>
</p>

<p>
    <a href="https://www.tiktok.com/@nanhongmanao" target="_blank" class="tiktok-link">
        <i class="fab fa-tiktok" aria-hidden="true"></i> TikTok
    </a>
</p>

<p>
    <a href="https://www.facebook.com" target="_blank" class="facebook-link">
        <i class="fab fa-facebook-f" aria-hidden="true"></i> Facebook
    </a>
</p>




    <p>
        <a href="mailto:guibaofangcom@gmail.com" class="contact-link">
            <i class="fa fa-envelope" aria-hidden="true"></i> Email: guibaofangcom@gmail.com
        </a>
    </p>
</div>

                    
                    
                </div>
            </div>
        </footer>
</body>
</html>';
        // 替换标题中的任何无效字符
        $safeTitle = preg_replace('/[^a-zA-Z0-9]+/', '', $translatedTitle);

        // 将标题转换为小写
        $safeTitle = strtolower($safeTitle);

        file_put_contents('articles/' . $safeTitle . '.html', $html);
    }
}
echo "上传的文章已经处理。";
} else {
echo "没有上传任何文章。";
}
?>

前台上传代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Upload Articles</title>
</head>
<body>
    <h1>Upload Articles</h1>
    <form action="上个文件.php" method="post" enctype="multipart/form-data">
        <input type="file" name="articles[]" multiple>
        <button type="submit" name="submit">Upload</button>
    </form>
</body>
</html>

有些简陋,需要的自己拿去美化

 演示:

上传的文章为

处理好的文章为:

可以看到关键词和描述已经自动调用

   

感谢观看,再次谢谢

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值