Reputation: 3
Hi I was wondering to add "" after/before a variable.
CODE:
<?php
while (false !== ($entry = readdir($handle))) {
$entry = "$entry";
$patterns = array();
$patterns[0] = '/.swf/';
$replacements = array();
$replacements[0] = ',';
echo preg_replace($patterns, $replacements, $entry);
}
?>
Echos out
word1,word2,word3,etc
I want it to echo out: "word1","word2","word3",etc instead How can you do it?
Upvotes: 0
Views: 591
Reputation: 6908
You can use an array and implode
<?php
$all = array();
while (false !== ($entry = readdir($handle)))
$all[] = '"'.str_replace('.swf', '', $entry).'"';
echo implode(', ', $all);
Upvotes: 0
Reputation: 39522
$string = '"' . implode('","', explode(',', $string)) . '"';
or simply str_replace
:
$string = '"' . str_replace(',', '","', $string) . '"';
Edit:
Is this what you're trying to do?
<?php
$entries = array();
while (($entry = readdir($handle)) !== false) {
if ($entry != '.' && $entry != '..') {
$entries[] = basename($entry);
}
}
echo '"' . implode('","', $entries) . '"';
?>
Upvotes: 3
Reputation: 3086
Just append them in your echo statement:
echo "\"" . preg_replace($patterns, $replacements, $entry) . "\"";
Upvotes: 0