Put quotation marks at the beginning and end of each number

-4

I need to put quotation marks at the beginning and end of each number.

Example:

I have the following sequence:

$seq = "123,456,789";

the result would have to stay like this

$seq = "'123','456','789'";
    
asked by anonymous 26.12.2018 / 19:14

4 answers

3

You can also do an Explode, Map and then Implode to transform, I'll send the example below:

 $myNumbers = "123,456,778";
 $myNumbersExploded = explode(",", $myNumbers);
 $myNumbersWithNewCaracter = array_map(function($v){ return "'".$v."'"; }, $myNumbersExploded);
 $myNumbers = implode(",", $myNumbersWithNewCaracter);

 var_dump($myNumbers);

see running on ideone

Output below:

    
26.12.2018 / 19:47
2

Other way of doing this:
$seq = "123,456,789";
$explode_seq = explode(',', $seq);
$n = array(); foreach ($explode_seq as $num){
$n[] = "'" . $num . "'";
}
$result = implode(" , ", $n);

    
01.01.2019 / 22:30
0

You can concatenate the single quotation mark at the beginning and end of the string and replace comma "," by "," "(single quotation mark + comma + single quotation mark) using php substr_replace.

link .

Any questions post there to see.

    
26.12.2018 / 19:29
0

You can use explode()

function colocaAspas($n){ return "'".$n."'"; }

$seq = "123,456,789";
$seq = array_map('colocaAspas', explode(',', $seq));

var_dump($seq);
    
26.12.2018 / 20:02