0

How can I best trim the following text outside the braces:

$myString = "10:0{ 'name':'John', 'age':30, 'car':null }24:0";

The text outside of the braces 10:0 and 24:0 may vary.

iancarp
  • 11
  • 3

2 Answers2

1

You can trim numbers and colons off with:

trim($myString, '0123456789:');

https://3v4l.org/vD7XC

... or you could replace everything outside the braces:

preg_replace('/.*?(\{.*?\}).*/', '$1', $myString);

https://3v4l.org/afN8B

user3783243
  • 5,368
  • 5
  • 22
  • 41
0

Just grab what's inside the braces:

preg_match('/{[^}]+}/', $myString, $match);
echo $match[0];
AbraCadaver
  • 78,200
  • 7
  • 66
  • 87