如何在php native中添加Apostrophe

时间:2015-09-09 05:53:21

标签: php

我有一个跟随字符串

$data = "20150825131738_262,20150825132227_241,20150825132254_898,20150825132320_209";

如何在'中添加$data。所以结果输出如下

'20150825131738_262','20150825132227_241','20150825132254_898','20150825132320_209,20150825132346_124','20150825132406_744','20150825143522_447','20150828145011_928'

帮我谢谢

5 个答案:

答案 0 :(得分:7)

以下会给你想要的结果

$data = "20150825131738_262,20150825132227_241,20150825132254_898,20150825132320_209";
$data_array = explode(',', $data);
$data = "'". implode("','", $data_array) . "'";
print_r($data);

看到它在线运行here

答案 1 :(得分:5)

试试这个

$arr=explode(',', $data);

implode("','",$arr);

答案 2 :(得分:1)

如果您从数据库或其他来源获取此值,则只需使用简单implodeexplode函数中的其中一种方式

$data = "20150825131738_262,20150825132227_241,20150825132254_898,20150825132320_209";
echo "'".implode("','",explode(',',$data))."'";

或使用preg_replace_callback作为

$data = "20150825131738_262,20150825132227_241,20150825132254_898,20150825132320_209";
echo preg_replace_callback('/[\d_]+/',function($match){ return "'$match[0]'";},$data);

答案 3 :(得分:1)

$data = "20150825131738_262,20150825132227_241,20150825132254_898,20150825132320_209";
$pad_data = "'" . $data . "'"; 
$data = str_replace (",", "', '", $pad_data);
echo $data;

答案 4 :(得分:0)

与其他答案略有不同的方法:

$data = '20150825131738_262,20150825132227_241,20150825132254_898,20150825132320_209';

# Split data into individual fields
$array = explode(',', $data);

# Surround each field with single quotes
$array = array_map(
    function ($field) { return "'${field}'"; },
    $array
);

# Join fields back into a single string again
$data = implode(',', $array);

echo $data;

可以看到代码正在运行here