如果没有图像,则显示无

时间:2014-01-24 21:24:29

标签: php image

如果没有图像,如何使下面的代码显示无?

  protected function DisplayPhoto($Sender) {
        $body = $Sender->EventArguments['Discussion']->Body;
        preg_match('#\<img(.+?)src=(.+?)\>#s', $body, $matches);
        $image = "src=" . $matches[2];
        echo '<img class="ProfilePhotoSmall"' . $image . '>';
  }

5 个答案:

答案 0 :(得分:1)

试试这个:

 protected function DisplayPhoto($Sender) {
            $body = $Sender->EventArguments['Discussion']->Body;
            preg_match('#\<img(.+?)src=(.+?)\>#s', $body, $matches);

            if(isset($matches[2])){
               $image = "src=" . $matches[2];
                 echo '<img class="ProfilePhotoSmall"' . $image . '>';
             }else{
                  //do what you want
                 }       
      }

答案 1 :(得分:0)

我可能会做一些事情:

if (count($matches) == 0){
    //do whatever if no image is here
}else{
    //do whatever if there IS an image
}

答案 2 :(得分:0)

protected function DisplayPhoto($Sender) {
    $body = $Sender->EventArguments['Discussion']->Body;
    preg_match('#\<img(.+?)src=(.+?)\>#s', $body, $matches);
    $image = "src=" . $matches[2];
    if( strlen( $matches[2] ) > 0 ){
        echo '<img class="ProfilePhotoSmall"' . $image . '>';

}

答案 3 :(得分:0)

尝试将代码更改为:

protected function DisplayPhoto($Sender) {
    $body = $Sender->EventArguments['Discussion']->Body;
    preg_match('#\<img(.+?)src=(.+?)\>#s', $body, $matches);
    $image = "src=" . $matches[2];
    if(!(strlen( $matches[2] ) > 0)){
        echo '<img class="ProfilePhotoSmall"' . $image . '>';
    }
}

答案 4 :(得分:0)

   If (strcmp($image,"src=")!=0)
      #print whatever you want

如果$匹配[2],您的$ images只包含“src =”。如果$ images只包含“src =”而不是什么都不做,否则打印你想要的任何内容。