Quote at the beginning and end of each number

Asked

Viewed 836 times

-3

I need to quote 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'";
  • 1

    And how did you try to do it? What was the result obtained?

4 answers

2


You can also make a Explode, Map and then Implode to transform, I will send below the example:

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

 var_dump($myNumbers);

see running on the ideone

Exit below:

inserir a descrição da imagem aqui

  • hit the variable in var_dump and took advantage put your example in ideone

  • Thank you @Leocaracciolo!

0

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

0

You can concatenate the single quote at the beginning and end of the string and replace the comma "," by " ',' " (single quote + comma + single quote) using php’s substr_replace.

http://php.net/manual/en/function.substr-replace.php.

Any doubt post there for us to see.

0

You can use explode()

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

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

var_dump($seq);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.