表1 page:(page_id,page_text,img_exist)=>(页面id,页面内容,是否存在图片1存在,0不存在)
表2 image:(img_id,img_name)=>(图片id,图片名)  
现在表2增加1列: image:(img_id,img_name,page_id)
现在要求是 把页面中含有的图片全部归类到属于哪一个页面id。
<?php$DBserver         = "localhost";
$DBname           = "new";
$DBuser           = "root";
$DBpassword       = "";$con = mysql_connect("localhost","root","");mysql_select_db("new");$query = mysql_query("select page_id,page_text from pagecontents where img_exist > 0") or die(mysql_error() );while($rows = mysql_fetch_array($query))
{
$pid = $rows['page_id'];
$ptext = $rows['page_text'];
$ptext = mysql_real_escape_string($ptext);
$ptext = preg_replace('/poweredby_mediawiki_88x31\.png/s','',$ptext);
 
if(preg_match_all('/.+\.(gif|swf|jpg|png)/is',$ptext,$match)){
$ptext = $match[1][0];
$ptext = mysql_real_escape_string($ptext);
}
mysql_select_db("lookdb",$con);
$sql = "update imgmulu set page_id = ('{$pid}')  where img_name = ('{$ptext}')";
mysql_query($sql) or die(mysql_error()); 
}
?>这样运行没效果,求高手指教下,谢谢帮助。

解决方案 »

  1.   

    imgmulu结构和image一样,是拿来测试的
      

  2.   

    echo  $ptext;  贴出结果
      

  3.   

    哦,正则错了,我的目的是需要把图片名截取出来
    <a href="/index.php/Image:ESIL_gongshi1.GIF" class="image" title="Image:ESIL_gongshi1.GIF"><img src="/images/2/25/ESIL_gongshi1.GIF" alt="Image:ESIL_gongshi1.GIF" width="314" height="44" longdesc="/index.php/Image:ESIL_gongshi1.GIF" /></a><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="550" height="400" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0"><param name="movie" value="/images/1/17/ESIL_xiaoshengqi2.swf"><param name="bgcolor" value="#FFFFFF"><param name="scale" value="exactfit"><embed src="/images/1/17/ESIL_xiaoshengqi2.swf" width="550" height="400" bgcolor="#FFFFFF" scale="exactfit" pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"></embed></object>
      

  4.   

    if(preg_match('/src="(.*?)"/is',$ptext,$match)){
        $ptext = $match[1];
        $ptext = mysql_real_escape_string($ptext);
    }
      

  5.   

    嗯,差不多了,还有一个问题就是。<?php$DBserver         = "localhost";
    $DBname           = "new";
    $DBuser           = "root";
    $DBpassword       = "";$con = mysql_connect("localhost","root","");mysql_query("set names 'gbk'");mysql_select_db("new");$query = mysql_query("select page_id,page_text from pagecontents where page_id = 100779") or die(mysql_error() );while($rows = mysql_fetch_array($query))
    {
    $pid = $rows['page_id'];
    $ptext = $rows['page_text'];
    $ptext = mysql_real_escape_string($ptext);
    $ptext = preg_replace('/\/images\/[\d]+\/[\d]+/s','../Documents/medies',$ptext);
    $ptext = mysql_real_escape_string($ptext);
    mysql_query("set names 'gbk'");
    $sql = "update pagecontents set page_text = ('{$ptext}') where page_id = ('{$pid}')"; 
    mysql_query($sql) or die(mysql_error());
    }
    ?>替换了相对路径再存入数据库,但是这时数据库的内容多了很多\r\n。这是什么原因呢?
    该怎么处理掉啊?