You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求PHP实现:从网站提取指定字符串后4字符并保存为plid.js

Got it, let's break down how to replicate your VB.NET functionality in PHP and set up a cron job for it—super straightforward once you know the steps!

Solution Overview

We'll split this into two key parts: writing a PHP script that mirrors your existing logic, then configuring a cron job to run it automatically on your server.


1. PHP Script Implementation

This script will handle fetching the target webpage, locating your specified string, extracting the 4 characters right after it, and saving the result to plid.js.

Here's a clean, functional version (customize the placeholders to match your needs):

<?php
// Replace these with your actual target URL and search string
$targetUrl = "https://your-target-site.com";
$searchString = "your-specified-search-text";

// Fetch webpage content (use cURL if file_get_contents is disabled)
$pageContent = file_get_contents($targetUrl);

if ($pageContent === false) {
    error_log("Failed to pull content from $targetUrl");
    exit(1);
}

// Find the position of your search string
$stringPos = strpos($pageContent, $searchString);

if ($stringPos === false) {
    error_log("Search string '$searchString' not found on the page");
    $result = ""; // Set a default value if needed
} else {
    // Calculate start position for the 4 characters post-search-string
    $startPos = $stringPos + strlen($searchString);
    $result = substr($pageContent, $startPos, 4);
}

// Save result to plid.js (use absolute path for cron reliability)
$savePath = "/var/www/html/plid.js"; // Update to your server's actual path
if (file_put_contents($savePath, $result) === false) {
    error_log("Couldn't write result to $savePath");
    exit(1);
}

exit(0);
?>

Quick Notes for the Script:

  • If your server blocks file_get_contents (common in restrictive environments), swap it for this cURL alternative:
    $ch = curl_init($targetUrl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // Follow redirects if needed
    $pageContent = curl_exec($ch);
    curl_close($ch);
    
  • Always use an absolute path for $savePath when running via cron—relative paths can break because cron uses a different working directory.
  • Make sure the directory where plid.js is saved has write permissions for the user running the cron job (usually your server account or web server user like www-data).

2. Set Up the Cron Job

Now let's schedule the script to run automatically:

  1. Open your crontab:
    Run this command in your server's terminal:

    crontab -e
    

    This will open your cron config in a text editor (nano is common).

  2. Add the cron rule:
    Add a line like this to define when the script runs. For example, to run it every day at 2 AM:

    0 2 * * * /usr/bin/php /path/to/your/script.php
    

    Let's break down the cron syntax:

    • 0: Minute (0 = top of the hour)
    • 2: Hour (2 = 2 AM, use 24-hour format)
    • *: Day of month (any day)
    • *: Month (any month)
    • *: Day of week (any day)
    • /usr/bin/php: Absolute path to your PHP executable (find this with which php if unsure)
    • /path/to/your/script.php: Absolute path to your PHP script
  3. Save and exit:
    In nano, press Ctrl+O to save, then Ctrl+X to exit. The cron job will now run automatically on your schedule.

Troubleshooting Cron:

  • Test the script manually first with php /path/to/your/script.php to confirm it works.
  • Check cron logs if things break—logs are usually at /var/log/cron (Debian/Ubuntu) or /var/log/messages (CentOS/RHEL).

内容的提问来源于stack exchange,提问作者user9370375

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 08:40:23