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'";
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'";
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);
Output below:
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);
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.
You can use explode()
function colocaAspas($n){ return "'".$n."'"; }
$seq = "123,456,789";
$seq = array_map('colocaAspas', explode(',', $seq));
var_dump($seq);