0

I have a this string: "["ST09390.2","ST62801.4"]" and I would like to create a array with this result, so I need a remove a first char " and last char ".

I try a trim($mndcheckout['ids'], """) but this is no solution.

JSExpress1
  • 35
  • 5

3 Answers3

1

I try a trim($mndcheckout['ids'], """) but this is no solution.

Yout have to escape the " as a \"

"["ST09390.2","ST62801.4"]"

Strange quoted .. try this:

$j = <<<STRING
"["ST09390.2","ST62801.4"]"
STRING;
$j = trim($j, "\"");

$a = json_decode($j);

print_r($a);
/*
Array
(
    [0] => ST09390.2
    [1] => ST62801.4
)
*/
hausl
  • 160
  • 16
0

You can directly str_replace function to remove unwanted stuff, If you aware about input string. You can refer below code for your expected result.

$str = '"["ST09390.2","ST62801.4"]"';
$str = str_replace('["','',$str);
$str = str_replace(']"','',$str);

$arr = explode(',', $str);
print_r($arr); 

This is tested code, I hope this will resolve your question.

Gajanan Kolpuke
  • 155
  • 1
  • 1
  • 14
0

If you are sure that the string is always in this format then you can use:

json_decode(substr($string, 1, -1),TRUE);

Have a look at http://php.net/manual/en/function.json-decode.php , http://php.net/substr . As is said in the answer to this question, How to get first 5 characters from string, for multi-byte strings (e.g. UTF-8, UTF-16, etc.) use mb_substr instead of substr.