dpnvrt3119 2016-03-28 16:39
浏览 35
已采纳

如果字符串为空,请让php sprintf省略return

I am wresting with printing shipping labels and getting the final output to omit a second address line if one exists. Is there a way to conditionally exclude the line break if $address2 is empty when using sprintf()?

            while($rows=mysql_fetch_array($result)){
                $name = $rows['address_name'];
                $address = $rows['street1'];
                $address2 = $rows['street2'];
                $citystatezip = $rows['city'].', '.$rows['state'].' '.$rows['zip'];
                $country = $rows['country'];
                $text = sprintf("%s
%s
%s
%s
", $name, $address, $address2, $citystatezip, $country);
                $pdf->Add_Label($text);
            }
  • 写回答

1条回答 默认 最新

  • douya1061 2016-03-28 17:22
    关注

    Your sprintf only has 4 '%s' and 5 params.

    In this code, I removed the from the sprintf into a conditional above.

            // Don't use mysql_xxxx functions
            // Should really be using PDO       
            //          +---------------+
            //          |               |
            while($rows=mysql_fetch_array($result)){
                $name = $rows['address_name'];
                $address = $rows['street1'];
                $address2 = $rows['street2'];
                $citystatezip = $rows['city'].', '.$rows['state'].' '.$rows['zip'];
                $country = $rows['country'];
                $addr2 = empty($address2) ? '' : $address2 . "
    ";
                $text = sprintf("%s
    %s
    %s%s
    %s
    ", $name, $address, $addr2, $citystatezip, $country);
                $pdf->Add_Label($text);
            }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥30 vue 页面窗口放大或者缩小元素会变化
  • ¥15 questasim仿真报错
  • ¥15 寻找电脑攻防的导师,有问题请教一下。
  • ¥20 微信同是win11,我的电脑安装不了pageoffice,一直无法打开
  • ¥15 这个界面我通过postman请求不到,但是通过浏览器可以正常访问