在每个单词周围加单引号


placing single quote around each words

我有一个以换行符分隔的单词列表,例如:

apple
ball
cat
dog

我想用逗号分隔每个单词,然后在每个单词周围加上单引号。

我可以去掉换行符并加上逗号,但是我不能用单引号把每个单词括起来。

我希望最终结果看起来像这样:

"苹果"、"球"、"猫"、"狗"等等。

下面是我的代码:
$words = getlist();
$str = preg_replace('#'s+#',',',trim($words));
echo $str;
function getlist($list)
{
    //words list
}

你可以,

$strings = explode(",",$str);
$str = "'".implode("','",$strings)."'";

这很简单:

$wordsArray = explode("'n", $words);
$result = "";
foreach ($wordsArray as $word) $result .= "'$word',";

你可以在这里看到演示:

http://ideone.com/SLzySu

在逗号(,)周围加双引号(""),如"','"

试试这个,

$words = 'asdf asdf aewrw werer ewrew';
$str = preg_replace('#'s+#',"','",trim($words));
echo "'" . $str . '"';

点击这里查看

这样做

$strong = explode("'n", $string);
$string = "'".implode("','",$strong)."'";
echo $string;
样本输出:

'apple','ball','cat','dog'

sed实用程序可以帮助您。

system('sed "s|'s|'',''|g" '.$tmpOutputFile .' >> '.$tmpOutputFile1);
system('sed "s|^|''|g" '.$tmpOutputFile1 .' >> '.$tmpOutputFile2);
system('sed "s|$|''|g" '.$tmpOutputFile2 .' >> '.$OutputFile);