一段代码执行有关问题

php中文网
发布: 2016-06-13 13:29:08
原创
858人浏览过

一段代码执行问题

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" /><script src="https://www.php.cn/hezuo/f220a8ffcae8732bbc8e3d8f7f36834b.js"></script>    
//<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 =<script src="https://www.php.cn/hezuo/f220a8ffcae8732bbc8e3d8f7f36834b.js"></script><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]&gt;805){
        $h=round(($m[2]*805)/$m[1]);
        $k=805;

        return "width=\"$k\" height=\"$h\" ";
    }
    return $m[0];
} <div class="clear"></div>
登录后复制
最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号