FrostyGamer150
FrostyGamer150

Reputation: 3

PHP add "" after/before a variable

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

Answers (3)

kelunik
kelunik

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

h2ooooooo
h2ooooooo

Reputation: 39522

By using explode and implode:

$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

EToreo
EToreo

Reputation: 3086

Just append them in your echo statement:

echo "\"" . preg_replace($patterns, $replacements, $entry) . "\"";

Upvotes: 0

Related Questions