0

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

代码:

<?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”等,而不是你怎么做?

4

3 回答 3

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) . '"';
?>
于 2013-10-29T20:40:48.473 回答
0

只需将它们附加到您的 echo 语句中:

echo "\"" . preg_replace($patterns, $replacements, $entry) . "\"";
于 2013-10-29T20:44:45.347 回答
0

您可以使用数组和implode

 <?php

    $all = array();

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

    echo implode(', ', $all);
于 2013-10-29T20:46:23.030 回答