PHP在变量之后/之前添加“”

时间:2013-10-29 20:39:20

标签: php

嗨我想知道在变量之后/之前添加“”。

CODE:

<?php
while (false !== ($entry = readdir($handle))) {
$entry = "$entry";
$patterns = array();
$patterns[0] = '/.swf/';
$replacements = array();
$replacements[0] = ',';
echo preg_replace($patterns, $replacements, $entry);
}
?>

回声

word1,word2,word3,etc

我想让它回应: “word1”,“word2”,“word3”等代替 你怎么能这样做?

3 个答案:

答案 0 :(得分:3)

使用explodeimplode

$string = '"' . implode('","', explode(',', $string)) . '"';

或只是str_replace

$string = '"' . str_replace(',', '","', $string) . '"';

编辑:

这是你想要做的吗?

<?php
    $entries = array();

    while (($entry = readdir($handle)) !== false) {
        if ($entry != '.' && $entry != '..') {
            $entries[] = basename($entry);
        }
    }

    echo '"' . implode('","', $entries) . '"';
?>

答案 1 :(得分:0)

只需在echo语句中附加它们:

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

答案 2 :(得分:0)

您可以使用数组和implode

 <?php

    $all = array();

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

    echo implode(', ', $all);