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.

3
  • 1
    Is it meant to be JSON? In which case just use json_decode. Perhaps provide a replication case of what your issue is and what you're trying to get to. Commented Oct 25, 2018 at 12:12
  • I want to create a array with this result. Commented Oct 25, 2018 at 12:14
  • One of these examples 3v4l.org/nLvHY ? Commented Oct 25, 2018 at 12:17

3 Answers 3

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
)
*/
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.