-1

Array ( [0] => assets/image/man.jpg [1] => assets/image/violin.jpg [2] => assets/image/test.txt )

The data from data base is like above.It contain images and txt.how can i display only images.

A.K.S
  • 97
  • 1
  • 4
  • 14

9 Answers9

1
$ar = ['assets/image/man.jpg','assets/image/violin.jpg','assets/image/test.txt'];
 $allowed = ['jpg']; //your image extensions
 $img_ar = [];
 foreach($ar as $img){
     $ext = pathinfo($img,PATHINFO_EXTENSION);
     if(in_array($ext,$allowed)){
         $img_ar[] = $img;
     }

 }
 print_r($img_ar);
Alex
  • 39
  • 4
0

Why do you check it when you want write it on page?
You can:
1. split assets/image/man.jpg with '/'
2. get last one,
3. split last one with '.'
4. get extension and if it was 'jpg' write it to page.

PrakashG
  • 1,642
  • 5
  • 20
  • 30
Bahman Shafiei
  • 395
  • 5
  • 22
0
$array= Array ( [0] => assets/image/man.jpg [1] => assets/image/violin.jpg [2] => assets/image/test.txt )
$m_array = preg_grep('/^.jpg\s.*/', $array);

$m_array contains matched elements of array.

For more detail have look at this thread search a php array for partial string match

Kamran Sohail
  • 602
  • 7
  • 13
0

Use foreach loop and get an extension of the file and display.

foreach($array_result as $result){

    //$array_result is array data
    //condition is checking the file that if it is an image or not
    $allowed =  array('gif','png' ,'jpg');
    $filename = $result;
    $ext = pathinfo($filename, PATHINFO_EXTENSION);
    if(in_array($ext, $allowed) ) {
        echo '<img src="'.$result.'" alt="" /> ';
    }
 }
Danish Ali
  • 2,354
  • 3
  • 15
  • 26
0

This should do the trick:

$images = array();
$images_exts = array('.jpg','.png');
foreach($db_array as $key => $value)
{ 
    foreach($images_exts as $ext)
    {
        if (strpos($value,$ext))
        {
            $images[] = $value;
        }
    }
}

Here is an example https://3v4l.org/8W3Be


And here is another way, whatever you like the most:

$images = array();
$images_exts = array('jpg','png');
foreach($input as $value)
{
    if(in_array(@end(explode('.', $value)), $images_exts))
    {
        $images[] = $value;
    }
}

Here is an example https://3v4l.org/b0njd

Sherif Salah
  • 2,085
  • 2
  • 9
  • 21
0

For this you can directly filter it when you are querying like

field like '%.jpg'

If you don't want to do that and manipulate the array you can use array_filter like,

$array= Array ('assets/image/man.jpg', 'assets/image/violin.jpg', 'assets/image/test.txt');

$output = array_filter($array, function($arr) {
        if (strpos($arr, '.jpg') == true){
                return $arr;
        }
});

$output array contains only the entries which having the .jpg string.

Here am using strpos to check .jpg exists or not.

you maybe use substr($str, -4) == '.jpg' to check the last 4characters.

Naveen K
  • 859
  • 4
  • 14
  • 29
0

If you are using PHP 5+ (which I hope you are on 7.0+), use SplFileInfo() class

$spl  = new SplFileInfo($fileName); 
if ($spl->getExtension() == 'jpg') {
//image
}
Rotimi
  • 4,783
  • 4
  • 18
  • 27
0

<?php
  $error = array();
  $file_extArr = explode(".", $file_name);
  $file_extEnd = end($file_extArr);
  $file_ext = strtolower($file_extEnd);

  $validateImage = array("png", "jpg", "jpeg", "gif");

  if (!in_array($file_ext, $validateImage)) {
    $error[] = "wrong format image";
  }

  if (!empty($error)) {
   return;
  }
?>
Hoàngg
  • 75
  • 2
0
    <?php 
    $data = Array ( 
       'assets/image/man.jpg ',
       'assets/image/violin.jpg ',
       'assets/image/test.txt ',
    );

    $arrDara = array();
    foreach ($data as $value) {
        $fileName = explode('/', $value);
        $arrDara[] = end($fileName);
    }

    print_r($arrDara);
?>

Just loop your array and explode every sting. the last index is what all you need.

Masih Ansari
  • 467
  • 3
  • 9