<?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 = 101370") or die(mysql_error() );
while($rows = mysql_fetch_array($query))
{
$pid = $rows['page_id'];
$ptext = $rows['page_text'];
//部分page_text
//<img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif" width="850" height="600" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />
//<img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif" width="550" height="400" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif" />
preg_match_all('#width="(\d+)"\s*height="(\d+)"#i',$ptext,$m);
if($m[1]>805){
$h=($m[2]*805)/$m[1] ;
$k=805;
    $ptext = preg_replace('/(width=)"(\d+)"(\s*height=)"(\d+)"/i','$1"'.$k.'"$3"'.$h.'"',$m);
}
mysql_query("set names 'gbk'");
$sql = "update pagecontents set page_text = ('{$ptext}') where page_id = ('{$pid}')";
mysql_query($sql) or die(mysql_error());
}
?>
结果是2句都执行了
//部分page_text
//<img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif" width="805" height="568" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />
//<img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif" width="805" height="568" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif" />本来想做的是只改width超过805的那条,也就是第一条,结果所有的都被替换了。

解决方案 »

  1.   

    <?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 = 101370") or die(mysql_error() );
    while($rows = mysql_fetch_array($query))
    {
        $pid = $rows['page_id'];
        $ptext = $rows['page_text'];
    //部分page_text    
    //<img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif" width="850" height="600" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />    
    //<img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif" width="550" height="400" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif" />  
              preg_match_all('#width="(\d+)"\s*height="(\d+)"#i',$ptext,$m);
    //$m[1]是width的值 , $m[2]是height的值。
        if($m[1]>805){
            $h=($m[2]*805)/$m[1] ;
            $k=805;        $ptext = preg_replace('/(width=)"(\d+)"(\s*height=)"(\d+)"/i','$1"'.$k.'"$3"'.$h.'"',$m);
        }
        mysql_query("set names 'gbk'");
        $sql = "update pagecontents set page_text = ('{$ptext}') where page_id = ('{$pid}')";
        mysql_query($sql) or die(mysql_error());
    }
    ?>
      

  2.   

    $m[1] 返回的是所有匹配的内容.print_r 看看你就知道了
      

  3.   

    preg_match_all('#width="(\d+)"\s*height="(\d+)"#i',$ptext,$m);
    //数组组合成字符串
    $m[1]=join(" ",$m[1]);
    $m[2]=join(" ",$m[2]);
    //$m[1]是width的值 , $m[2]是height的值。
        if($m[1]>805){
            $h=($m[2]*805)/$m[1] ;
            $k=805;        $ptext = preg_replace('/(width=)"(\d+)"(\s*height=)"(\d+)"/i','$1"'.$k.'"$3"'.$h.'"',$m);
        }
      

  4.   

    记得我给你写贴过代码$page_text =<<< TXT
    <img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif" width="850" height="600" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />    
    <img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif" width="550" height="400" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif" />  
    TXT;echo preg_replace_callback('#width="(\d+)"\s*height="(\d+)"#i', 'back', $page_text );//$m[1]是width的值 , $m[2]是height的值。
    function back($m) {
        if($m[1]>805){
            $h=round(($m[2]*805)/$m[1]);
            $k=805;        return "width=\"$k\" height=\"$h\" ";
        }
        return $m[0];
    }
    <img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif" width="805" height="568"  longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />    
    <img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif" width="550" height="400" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif" />  
      

  5.   

    Warning: preg_replace_callback() [function.preg-replace-callback]: Requires argument 2, 'back', to be a valid callback in :
    echo preg_replace_callback('#width="(\d+)"\s*height="(\d+)"#i', 'back', $page_text );
      

  6.   

    <?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 = 101370") or die(mysql_error() );
    while($rows = mysql_fetch_array($query))
    {
    $pid = $rows['page_id'];
    $ptext = $rows['page_text'];

    //部分page_text($ptext)
    //<img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif" width="850" height="600" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />
    //<img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif" width="550" height="400" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif" />
    //<img src="../Documents/medies/ESIL_shishiranshao.gif" alt="Image:ESIL_shishiranshao.gif" width="850" height="600" longdesc="/index.php/Image:ESIL_shishiranshao.gif" />
    preg_match_all('#width="(\d+)"\s*height="(\d+)"#i',$ptext,$m);
    $m[1]=join(" ",$m[1]);
    $m[2]=join(" ",$m[2]);
    function back($m){
    if($m[1]>805){
    $h=round(($m[2]*805)/$m[1]);
    $k=805;
    return  "width=\"$k\" height=\"$h\" ";
    }
    return $m[0];
    }
        $ptext = preg_replace_callback('#width="(\d+)"\s*height="(\d+)"#i','back', $ptext );
    mysql_query("set names 'gbk'");
    $sql = "update pagecontents set page_text = ('{$ptext}') where page_id = 101370";
    mysql_query($sql) or die(mysql_error());
    }
    ?>这样解决了。