JSExpress1
JSExpress1

Reputation: 35

remove char '"' in string and create array in php

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.

Upvotes: 0

Views: 46

Answers (3)

Mihai Asandulesei
Mihai Asandulesei

Reputation: 11

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.

Upvotes: 0

Gajanan Kolpuke
Gajanan Kolpuke

Reputation: 155

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.

Upvotes: 0

hausl
hausl

Reputation: 180

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
)
*/

Upvotes: 1

Related Questions