2

I have this string

{"CALL CENTER":"CALL CENTER"}

I need to print CALL CENTER and I have tried using

substr($mystring, strpos($mystring, ":") + 1)

It gives me "CALL CENTER"}

How can I remove the special character from the result?

Andreas
  • 23,610
  • 6
  • 30
  • 62
may
  • 675
  • 1
  • 10
  • 26

2 Answers2

5

Use json_decode with the assoc array flag set to true and then you can just access the data in the array instead of parsing the string yourself.

$jsonArray = json_decode('{"CALL CENTER":"CALL CENTER"}', true);
echo $jsonArray['CALL CENTER']; // CALL CENTER

$jsonArray = json_decode('{"CALL CENTER":"CALL CENTER 2"}', true);
echo $jsonArray['CALL CENTER']; // CALL CENTER 2

http://sandbox.onlinephpfunctions.com/code/7650e1b9e705318e31c2b02f44ed05f9ee201d13

JasonB
  • 6,243
  • 2
  • 17
  • 27
0

You can use the trim() function:

$mystring = trim($mystring, '"}');

Documentation: http://php.net/manual/en/function.trim.php

Steven
  • 381
  • 2
  • 10
  • I still get the quotes "" – may Jan 15 '18 at 04:16
  • The second parameter is a list of characters, put " into the list and they will trimed from the front and back of the string: `trim($str, '"}');` – Steven Jan 15 '18 at 04:20
  • Don't use trim or any other string handling functions to decode json. The string is probably coming from an API and can expand and become larger any day. Handle it as a json, anything else is foolish in my opinion. – Andreas Jan 15 '18 at 04:22
  • @Andreas When you expect JSON-Data, then this is true. But: this was not clear from the question, just that the string contains a JSON-like format (btw: transform JSON-Data without error-handling is a bit foolish too, didn't you think so?) – Steven Jan 15 '18 at 04:29
  • "when you expect json data"? It is json. Period. I would be surprised if it's a string with both json and "other text" combined in that string. And even if it is, remove the "text" and handle the rest as json. – Andreas Jan 15 '18 at 04:33