首頁 >後端開發 >php教程 >一段代码执行有关问题

一段代码执行有关问题

WBOY
WBOY原創
2016-06-13 10:25:43924瀏覽

一段代码执行问题

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--><?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"    style="max-width:90%"  style="max-width:90%" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif" />    //<img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif"    style="max-width:90%"  style="max-width:90%" 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句都执行了
HTML code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->//部分page_text    //<img src="../Documents/medies/ESIL_zhaoqifadianrelianghuishou.gif" alt="Image:ESIL_zhaoqifadianrelianghuishou.gif"    style="max-width:90%"  style="max-width:90%" longdesc="/index.php/Image:ESIL_zhaoqifadianrelianghuishou.gif">    //<img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif"    style="max-width:90%"  style="max-width:90%" longdesc="/index.php/Image:ESIL_gaowenjiayarechuliliucheng1.gif">


本来想做的是只改width超过805的那条,也就是第一条,结果所有的都被替换了。

------解决方案--------------------
$m[1] 返回的是所有匹配的内容.

print_r 看看你就知道了
------解决方案--------------------
问题还没解决呢? 你贴了几段。到底以那段为标准。问题也没描述清楚。
------解决方案--------------------
记得我给你写贴过代码
PHP code
$page_text =    <img src="../Documents/medies/ESIL_gaowenjiayarechuliliucheng1.gif" alt="Image:ESIL_gaowenjiayarechuliliucheng1.gif"    style="max-width:90%"  style="max-width:90%" 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];}<div class="clear">
                 
              
              
        
            </div>
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn