以下是一个示例 PHP 函数,用于抓取 HTML 管家婆王中王期期准十m 内容中的图片路径,并替换成指定的路径:
function replaceImagePaths($htmlContent, $replacementPath) { // 使用DOM解析HTML内容 $dom = new DOMDocument(); @$dom->loadHTML($htmlContent); // 获取所有图片标签 $images = $dom->getElementsByTagName('img'); // 遍历每个图片标签 foreach ($images as $image) { $currentSrc = $image->getAttribute('src'); // 替换图片路径 $newSrc = str_replace($currentSrc, $replacementPath, $currentSrc); $image->setAttribute('src', $newSrc); } // 返回替换后的HTML内容 return $dom->saveHTML(); } // 示例用法 $htmlContent = '<html><body><img src=http://www.yncoders.com/show/"old/path/image.jpg"></body></html>'; $replacementPath = 'new/path/image.jpg'; $newHtmlContent = replaceImagePaths($htmlContent, $replacementPath); echo $newHtmlContent;
此函数使用 香港全年资料大全香港东方新经 PHP 的 DOMDocument 类来解析 HTML 内容,然后遍历所有图片标签,并将其路径替换为指定的路径。最后返回替换后的 HTML 内容。