2013-10-29 110 views
0

您好我想知道在变量之后/之前添加“”。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 

我希望它呼应了: “字词1”, “单词2”, “WORD3” 等代替 你怎么能做到这一点?

回答

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) . '"'; 
?> 
+0

这就是我想要的,但它回声了这一点: “字词1”,” word2,“等 – FrostyGamer150

+0

@ FrostyGamer150看看我的编辑。那是你想要做什么? – h2ooooooo

+0

不是,你发布的第一个很棒,但是我的来了is.word1,“”word2“等等我想要它是”word1“,”word2“等 – FrostyGamer150

0

您只需将它们放在你的回声声明:

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

您可以使用数组和implode

<?php 

    $all = array(); 

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

    echo implode(', ', $all); 
相关问题