我有一个动态的php代码如下
$url = "http://www.myweb.com/home";
$mycontents = 'These are the contents with <a href="myweb/local.php" />relative link</a> and <a href="http://www.myweb.com/home/loc.php"> absolute link</a> and some images with <img src="myweb/local.jpg" /> with relative link and <img src="http://www.myurl.com/pictures/pic.jpg" />';
$url
并且$mycontents
根据获取的数据而变化,甚至链接也包含变化。任何将扫描变量的函数,$mycontents
如果发现任何带有 a 或 src 的链接,首先检查是否是绝对的,如果是,则保留它,如果不是,使用 str_replacestr_replace("src=\"", "src=\"".$url, $mycontents)
并在回显 $mycontents 之前更改所有链接,目标是一次$mycontents 得到回显,必须通过扫描所有相对链接并发现更改为绝对链接进行修改。