我有字符串$text = " /var/www/images/mobile/test/test/, 1346744549";
,我需要将其转换为$text = " '/var/www/images/mobile/test/test/', '1346744549'";
- 将'添加到字符串中的每个“值”。问题出现斜杠,我不知道如何识别它。这是我的样本,但它已经错了......
$text = " /var/www/images/mobile/test/test/, 1346744549";
$text = preg_replace("/\b|\/\b/i", '"', $text);
echo $text;
答案 0 :(得分:1)
这是功能:
"'".implode("','",explode(',',$text))."'";
您可以在此处查看结果:http://sandbox.onlinephpfunctions.com/code/14ed966d086494933f0e0ff48230083623a9c527
答案 1 :(得分:1)
尝试
$text = " /var/www/images/mobile/test/test/, 1346744549";
$text = preg_replace("/[^\s,]+/", "'$0'", $text);
echo $text;
[^\s,]+
匹配任何系列的非空格,非逗号字符本身,但周围有'
($ 0是匹配)
如果您希望允许数据中的空格,请尝试使用
$text = " /var/www/images/mobile/test/test/, 1346744549, Hello Foobar test";
$text = preg_replace("/(^\s*|,\s*)([^,]+)/", "$1'$2'", $text);
echo $text;
将输出
'/ var / www / images / mobile / test / test /','1346744549','Hello Foobar test'
答案 2 :(得分:0)
$text = " /var/www/images/mobile/test/test/, 1346744549";
echo preg_replace('/(?=[,\s]|$|^)/i', '"', $text);