-1

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"

gen_Eric
  • 223,194
  • 41
  • 299
  • 337
theorise
  • 7,245
  • 14
  • 46
  • 65
  • If in doubt, consult php.net on what you're using. It's very easy to search, just go "php.net/var_dump" and it takes you to the page. For printing you should use echo or print. If you're new, check over the manual section on the basics -- it's all on the site. – Incognito Mar 04 '11 at 14:31

3 Answers3

3

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);
Stefan Gehrig
  • 82,642
  • 24
  • 155
  • 189
  • 1
    Never trust what you see in the browser's rendered output when debugging PHP scripts. Always look at the page source, which shows the raw un-interpreted output. – Marc B Mar 04 '11 at 14:39
3

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);
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
3

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); 
hsz
  • 148,279
  • 62
  • 259
  • 315