1

How to make the code below to display none if no 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 . '>'; } 
0

5 Answers 5

1

Try this:

 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 } } 
Sign up to request clarification or add additional context in comments.

Comments

0

I'd probably do something along the lines of:

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

Comments

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 . '>'; } 

Comments

0

Try changing the code to:

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 . '>'; } } 

Comments

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

If $matches[2] your $images contain only "src=". If $images only contains "src=" than do nothing,else print whatever you want.

1 Comment

If ($matches[2] is empty,than $images will only be src=. If so print nothing,else print whatever you want.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.