0

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?

0

3 Answers 3

3

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) . '"';
?>
Sign up to request clarification or add additional context in comments.

4 Comments

Thats what I want but it echos out this:"word1,""word2,"etc
@FrostyGamer150 Check out my edit. Is that what you're trying to do?
Nope, the first one you posted is great but my come is gone.word1,""word2,"etc i want it to be "word1","word2",etc
NVM. I fixed it, I edit MY code. Your code worked great! :D Thanks
0

Just append them in your echo statement:

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

Comments

0

You can use an array and implode

 <?php

    $all = array();

    while (false !== ($entry = readdir($handle)))
        $all[] = '"'.str_replace('.swf', '', $entry).'"';

    echo implode(', ', $all);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.