ereg('(<img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',$str,$regs)
$str="[localimg=".$regs[2].",".$regs[4].",".$regs[6]."]"
试试

解决方案 »

  1.   

    偶用SimpleXML做,可以忽略很多不必要的复杂性$str = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachimg_235058" width="321" height="199" alt="" />';
    $xml = new SimpleXMLElement($str);preg_match("/\d+$/", $xml[aid], $aid);$out = "[localing={$aid[0]},{$xml[width]},{$xml[height]}]";
    echo $out."\n";
      

  2.   

    $str = preg_replace('(<img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',"[localimg=".$2.",".$4.",".$6."]" ,$str)
      

  3.   

    $str = preg_replace('( <img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',"[localimg=$2,$4,$6]" ,$str)
    这个试试
      

  4.   

    $str = preg_replace('(<img.*?aid.*?)(\d+)(.*?width.*?)(\d+)(.*?height.*?)(\d+)(.*?/>)',"[localimg=$2,$4,$6]" ,$str) 
    这个试试
      

  5.   

    非常感谢2位老兄的帮忙,特别是myvicy老兄!!
    实际上我就是想用preg_replace转换的,但是利用了myvicy老兄的代码,程序执行报错,还请帮忙!!
    Warning: preg_replace() [function.preg-replace]: Unknown modifier '(
      

  6.   


    $str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/','[localimg=$1,$2,$3]',$str);
      

  7.   

    还有发的代码有可能csdn在正则中自动加空格,如果发现有请删除一下.
      

  8.   

    利用下面代码搞定!!$str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/','[localimg=$1,$2,$3]',$str);
    不过现在遇到新的问题了
    <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />
    <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />
    <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" height="199" alt="" />
    <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100004" alt="" />如何实现不含有width或height的img标签转为
    [localimg=235058,0,0] 
    [localimg=235058,321,0] 
    [localimg=235058,0,199] 
    谢谢!!!
      

  9.   

    <?php
    //下面的$str是你要分析的签
    $str = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100003" height="199" alt="" />';
    $xml = new SimpleXMLElement($str);
    preg_match("/\d+$/", $xml[aid], $aid);
    //进行判断,没有设0
    if($xml[width]=="")$xml[width]=0;
    if($xml[height]=="")$xml[height]=0;
    $out = "[localing={$aid[0]},{$xml[width]},{$xml[height]}]";
    echo $out."\n";
    ?> 
      

  10.   

    <?php
      $text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';
      $text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';
      
      // 回调函数
      function formatimg($matches) {
       if($2=="")$2=0
       if($3=="")$3=0
       return "[localimg=".$1.",".$2.",",$3."]"
      }  echo preg_replace_callback(
                  "/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/",
                  "formatimg",
                  $text);
    ?>
      

  11.   

    <?php
      $text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';
      $text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';
      
      // 回调函数
      function formatimg($matches) {
       if($2=="")$2=0
       if($3=="")$3=0
       return "[localimg=".$1.",".$2.",".$3."]"
      }  echo preg_replace_callback(
                  "/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/",
                  "formatimg",
                  $text);
    ?>
      

  12.   

    这要执行二次替换才行
    $str=preg_replace('/<img.*?aid="?[^"]*_(\d+)"?\s*(?:width="?([^"]+)"?)?\s*(?:height="?([^"]+)"?)?[^>]+>/','[localimg=$1,$2,$3]',$str);
    $str=preg_replace('/((?<=,)(?=,)|(?<=,)(?=\]))/','0',$str);
      

  13.   

    <?php
      $text = '<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100001" width="321" height="199" alt="" />';
      $text.='<img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" aid="attachment_100002" width="321" alt="" />';
      
      // 回调函数
      function formatimg($matches) {
       if($matches[2]=="")$matches[2]=0
       if($matches[3]=="")$matches[3]=0
       return "[localimg=".$matches[1].",".$matches[2].",".$matches[3]."]"
      }  echo preg_replace_callback(
                  "/<img.*?aid="?[^"]*_(\d+)"?\s+width="?([^"]+)"?\s+height="?([^"]+)"?[^>]+>/",
                  "formatimg",
                  $text);
    ?>
      

  14.   

    非常感谢myvicy,我继续学习,现在先结帖!!!
      

  15.   

    $text = ' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" ai
    d="attachment_100001" width="321" height="199" alt="" />';
    //$text.=' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" a
    id="attachment_100002" width="321" alt="" />';
    //$text.=' <img src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" border="0" a
    id="attachment_100003" alt="" />';
    if(preg_match_all('/<img.*?aid="?[^"]*_(\d+)"?\s+?(?:width="?([^"]+)"?)?(?:\s+)?(?:height="?([^"]+)"?)?[^>]+>/',$text,$s)){
      $c = count($s);
      $r = count($s[0]);
      foreach(range(1,$r) as $num){
        $mm =array();
        for ($i = 1;$i< $c;$i++) {
            $mm[] = $s[$i][$num-1]|0;
        }
        $result[] = '[localimg='.join(",",$mm).']';
      }
      print_r($result);
    }
      

  16.   

    不好意思,已经结帖了,明天再来给你加分!!!
    /Aylazhang
      

  17.   

    首先谢谢各位老兄的大力帮助,不过现在遇到了新的问题,还是请各位老兄继续能帮忙!!我发现我的img代码并不是很规则,现在遇到的情况就是:
    <img height="199" src="http://www.discuz.net/attachments/month_0803/20080311_016edd593adc1dddc92eYbiEXWHyCwMZ.gif" width="321" border="0" aid="attachment_100001" />
    类似与这样的标签如何转换,谢谢!!