日期:2014-05-17  浏览次数:20492 次

一小段PHP代码问题
PHP code


<?php 

$txt =<<< TXT
"<img src="../Documents/medies/Link_icon1.gif" alt="Image:link_icon1.gif" width="16" height="16" longdesc="/index.php/Image:Link_icon1.gif" /></a><a href="/index.php/%E7%A8%80%E7%A1%9D%E9%85%B8%E6%B0%A7%E5%8C%96%E6%B3%95" title="稀硝酸氧化法">稀硝酸氧化法</a>
<img src="../Documents/medies/Link_icon.gif" alt="Image:link_icon.gif" width="16" height="16" longdesc="/index.php/Image:Link_icon.gif" /></a><a href="/index.php/%E6%B0%A8-%E7%A2%B1%E6%BA%B6%E6%B6%B2%E4%B8%A4%E7%BA%A7%E5%90%B8%E6%94%B6%E6%B3%95" title="氨-碱溶液两级吸收法">氨-碱溶液两级吸收法</a> 
TXT;



if(preg_match_all('/src="(.*?)"../Documents/medies/is',$txt,$m)){
    $imgne = $m[0];
}
print_r ($imgne);
?>




这样正则好像不对,我就想了另一个折中的办法:

PHP code

<?php 

$txt =<<< TXT
"<img src="../Documents/medies/Link_icon1.gif" alt="Image:link_icon1.gif" width="16" height="16" longdesc="/index.php/Image:Link_icon1.gif" /></a><a href="/index.php/%E7%A8%80%E7%A1%9D%E9%85%B8%E6%B0%A7%E5%8C%96%E6%B3%95" title="稀硝酸氧化法">稀硝酸氧化法</a>
<img src="../Documents/medies/Link_icon.gif" alt="Image:link_icon.gif" width="16" height="16" longdesc="/index.php/Image:Link_icon.gif" /></a><a href="/index.php/%E6%B0%A8-%E7%A2%B1%E6%BA%B6%E6%B6%B2%E4%B8%A4%E7%BA%A7%E5%90%B8%E6%94%B6%E6%B3%95" title="氨-碱溶液两级吸收法">氨-碱溶液两级吸收法</a> 
TXT;


if(preg_match_all('/src="(.*?)"/is',$txt,$m)){
    $imgne = $m[0];
    $imgne = preg_replace('../Documents/medies/','',$imgne);
}
print_r ($imgne);
?>


这样的正则能得到../Documents/medies/Link_icon1.gif和../Documents/medies/Link_icon.gif
于是我想再替换掉。可是报错:
Warning: preg_replace() [function.preg-replace]: Unknown modifier '/' in 


需要得到的是Link_icon1.gif和Link_icon.gif。前面的../Documents/medies/路径是固定的。
该怎么做呢?



------解决方案--------------------
$s=join("','",$imgne);
$sql = "update imgmulu set page_id = 101370 where img_name in ('". $s."')";