doufan6544 2011-11-20 19:54
浏览 64

在HTML页面中使用新名称查找并替换所有出现的旧图像名称

After I rename an image I would like to perform a search on all HTML Pages within a directory. The idea is to find all pages with the old name and update the page with new name.

I am having a problem with the string replacement portion.

$files = glob('/directory/*.html');
foreach($files AS $file) {
   $html = file_get_contents($file);
   $find = "old.jpg";
   preg_match("#src=(\"?|'?)(http://www.domain.com/images/|/images/)?$find(\"?|'?)#si", $html, $image);
   if (!empty($image[1])) {

       //find and replace all occurrences within page I need help with.

       $write = fopen($file, 'w');
       fwrite($write, $new_html);
       fclose($write);
    }
}

Examples of what I am searching for and replacing.

Find src="/images/old.jpg" replace src="/images/new.jpg"

Find src="http://www.domain.com/images/old.jpg replace src="http://www.domain.com/images/new.jpg"

Find src="/old.jpg ignore

Find src="http://www.anyotherdomain/old.jpg ignore

  • 写回答

2条回答 默认 最新

  • dsbx40787736 2011-11-20 20:07
    关注

    A FAR superior solution would be to include a constant with the image name in every webpage and use ti to reference your image path.

    constants.inc.php

    <?php
        define('IMAGE_PATH', 'absolute/path/to/image.jpg');
    ?>
    

    anyotherpage.php

    <?php require_once('constants.inc.php'); ?>
    ...
    ...
    ...
    <img src="<?php echo IMAGE_PATH ?>">
    
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b