php怎么使用图像域做链接,更改所有img链接以指向PHP中无烹饪域的理想方法

SED解决方案:

对所有HTML文件使用SED。您可以使用PHP(或脚本函数)遍历所有文件。

glob

是一个很好的功能。

// this sed assumes you have

// example.png

// and want to change it to

// example.png

// Loop through each .html file in current directory and create an .html2

// If satisfied w results can finalize .html2 files

// I use .html2 so you don't overwrite your original files

foreach (glob("*.html") as $file)

{

// You have to escape double quotes

$lookFor = 'src=\"imgs';

$replaceWith = 'src=\"http://www.noCookies.com/imgs';

// create the sed command:

// _g is to do a global search

$shellCommand = "sed s_{$lookFor}_{$replaceWith}_g {$file}";

// create a NEW file and open it

$fp = fopen("{$file}2", "w");

// preform sed and write it to the new file

fwrite($fp, shell_exec($shellCommand));

fclose($fp);

}

?>

编辑-原始答案如下:

另一个选项是mod rewrite,它可能和原来的一样慢或慢。

如果您运行apache,并且所有的图像都在一个单独的文件夹中(即使它们不是,但这样做更有效),那么您可以使用mod重写…像这样的……?我有点生疏,但你想用相对路径,用绝对路径切换:

RewriteEngine on

RewriteRule ^media/images/(.*) http://noCookies.com/media/images/$1 [L]

我想这绝对是一种方式。然后,您可以随着时间的推移缓慢地更改旧的img源。或者只保留旧的图像,并为您添加的任何新图像添加正确的img-src。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值