1

我有字符串$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;
4

3 回答 3

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 测试'

于 2012-09-04T08:22:33.297 回答
1

这是功能:

"'".implode("','",explode(',',$text))."'";

你可以在这里看到结果:http: //sandbox.onlinephpfunctions.com/code/14ed966d086494933f0e0ff48230083623a9c527

于 2012-09-04T08:06:15.313 回答
0
$text = " /var/www/images/mobile/test/test/, 1346744549";
echo preg_replace('/(?=[,\s]|$|^)/i', '"', $text);
于 2012-09-04T08:19:06.100 回答