Alex Alex - 4 months ago 8
PHP Question

Separate names with single quote and commas in php

I have a directory where it content are saved in my database both files and sub dir name, now i have tried to select all to sub directories name so i can use it in sql select query but my problem is the selected dir show like this

cssjspageimages
.
How can i separate each dir name with commas and quote and also not add comma in last name?
Example of how i want it to look like
'css','js','page','images'


<?php
$dirs = array_filter(glob('*'), 'is_dir');

if(!empty($dirs)){
$listdirs = '';
foreach ($dirs as $fileinfo){
$result = end(explode('/', $fileinfo));
$listdirs .= $result;
echo $listdirs;
}
}
?>

Answer

Maybe this:

<?php

$dirs = array_filter(glob('*'), 'is_dir');

if (!empty($dirs)) {
  $listdirs = '';

  foreach ($dirs as $fileinfo) {
      $result = end(explode('/', $fileinfo)); 
      $listdirs .= '\'' . $result . '\','; /* Enclose the $result in quotes (') and append a comma (,) */
  }

  $listdirs = rtrim($listdirs, ','); /* Strip the trailling comma */
  echo $listdirs;     
}
Comments