代码片段仓库

收集、管理和分享有用的代码片段,提高开发效率

代码片段

PHP Traits PHP

代码复用特性

<?php

trait Logger {
    public function log($message) {
        echo date('Y-m-d H:i:s') . ' ' . $message . PHP_EOL;
    }
}

class User {
    use Logger;
    
    public function save() {
        $this->log('Saving user');
        // Save logic
    }
}

class Product {
    use Logger;
    
    public function update() {
        $this->log('Updating product');
    }
}

$user = new User();
$user->save();

$prod = new Product();
$prod->update();
Python类型 Python

类型注释示例

from typing import List, Tuple, Dict, Optional

def greet(name: str) -> str:
    return f"Hello, {name}!"

def process_data(data: List[int]) -> float:
    return sum(data) / len(data)

User = Dict[str, int]
Address = Tuple[str, int, str]

def get_user(user_id: int) -> Optional[User]:
    if user_id == 1:
        return {"name": "Alice", "age": 30}
    return None
Java文件压缩 Java

ZIP压缩文件

import java.io.*;
import java.util.zip.*;

public class Zmpper {
    public static void compressFile(String sourceFile, String zipFile) throws IOException {
        try (FileOutputStream fos = new FileOutputStream(zipFile);
             ZipOutputStream zos = new ZipOutputStream(fos)) {
            File file = new File(sourceFile);
            try (FileInputStream fis = new FileInputStream(file)) {
                ZipEntry zeEntry = new ZipEntry(file.getName());
                zos.putNextEntry(zeEntry);
                byte[] buffer = new byte[1024];
                int len;
                while ((len = fis.read(buffer)) > 0) {
                    zos.write(buffer, 0, len);
                }
            }
        }
    }
}
C++文件加密 C++

XOR简单加密

#include <fstream>
#include <iostream>
#include <string>

using namespace std;

void xorEncrypt(const string& inputFile, const string& outputFile, char key) {
    ifstream in(inputFile, ios::brary|ios::in);
    ofstream out(outputFile, ios::binary|ios::out);
    
    char ch;
    while (in.get(ch)) {
        ch ^= key;
        out.put(ch);
    }
}

int main() {
    xorEncrypt("input.txt", "encrypted.bin", 'x');
    return 0;
}
PHP GD库 PHP

创建缩略图

🔥             
Python爬虫 Python

BeautifulSoup解析

import requests
from bs4 import BeautifulSoup

response = requests.get('https://example.com')
soup = BeautifulSoup(response.text, 'html.parser')

title = soup.title.text
print(f'Title: {title}')

for link in soup.find_all('a'):
    print(link.get('href', None))

为什么选择CodeSnippets?

高效管理您的代码片段,提高开发效率

智能搜索

通过关键字、语言或分类快速查找代码片段,支持模糊搜索和过滤功能

语法高亮

支持多种编程语言的语法高亮,使代码更加清晰易读

多设备同步

随时随地访问您的代码片段库,支持桌面和移动设备

热门分类

浏览最受欢迎的代码分类