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

使用simple_html_dom解析表格并批量替换表格内URL后缀

Got it, let's work through this solution since you're already using Simple HTML DOM for table parsing. Adapting this to handle dynamic content (no fixed filenames needed) is totally doable—here's a clean, robust approach:

Step-by-Step Implementation

First, make sure you're loading the latest dynamic HTML content (whether it's fetched from a URL, generated dynamically, or pulled from a string). Then target only the links inside table cells to avoid modifying other parts of the page:

// Include the Simple HTML DOM library first
include('simple_html_dom.php');

// Load your dynamic HTML content (replace this with your actual content source)
// Example 1: Fetch from a dynamic URL
$html = file_get_html('https://your-dynamic-content-url.com');
// Example 2: Load from a dynamically generated string
// $html = str_get_html($yourDynamicHtmlString);

// Loop through every <a> tag inside table cells
foreach ($html->find('table td a') as $linkElement) {
    // Grab the current href value
    $originalHref = $linkElement->href;
    
    // Replace .htm (case-insensitive) with .php at the end of the URL
    // Use preg_replace to handle .HTM, .Htm, etc.
    $updatedHref = preg_replace('/\.htm$/i', '.php', $originalHref);
    
    // Update the href attribute in the DOM
    $linkElement->href = $updatedHref;
}

// Output the modified HTML, or save it somewhere
echo $html;
// Or save to a file if needed: file_put_contents('updated-content.html', $html);

Key Details for Dynamic Scenarios

  • No fixed filename dependency: By loading the HTML dynamically each time (either via URL or string), you'll always process the latest content—no need to hardcode filenames.
  • Precise targeting: The table td a selector ensures you only modify links inside table cells, not other links on the page.
  • Case flexibility: The i flag in the regex handles uppercase variations like .HTM or .Htm—if you know all links use lowercase .htm, you can swap preg_replace for a simpler str_replace('.htm', '.php', $originalHref).
  • Works with query parameters: The regex targets .htm at the end of the URL, so links like page.htm?id=123 will become page.php?id=123 correctly.

Just make sure you're reloading the HTML content each time you run this script to pick up any dynamic changes to the source.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.22 07:48:10