I am using a foreach loop and a var_dump but the var_dump from the following code outputs something strange. How do I get rid of the pre-prended sring() and quotation marks?
$dir = 'url/dir/dir/';
$images_array = glob($dir.'*.jpg');
$images = array();
foreach ($images_array as $image) {
$images[] = str_replace($dir, '', $image);
}
var_dump(implode(',', $images));
Output:
string(51) "image1.jpg,image2.jpg,image3.jpg,image4.jpg"
That's what var_dump
does - it prints the datatype and the length. If you want to output just the string use
echo implode(',', $images);
var_dump
isn't outputting anything 'strange'. That is what it's supposed to do. It's for debugging, not for echoing.
Just echo
the string you want:
echo implode(',', $images);
var_dump
returns you type of variable and all information about it.
If you use it with HTML <pre>
echo '<pre>';
var_dump($images);
it will print for you an array with all elements in new lines.
If:
echo '<pre>';
var_dump(implode(',', $images));
it returns string.
And also shows you that it is a string.
If you just want to print value, use echo
:
echo implode(',', $images);