如何从字符串中删除前导和尾随字符?

How to remove leading and trailing characters from a string?

我有一个歪数据"""" { ... } """ 我想稍微润色一下。

如何在打开 { 之前取消 " 并在关闭 } 之后取消 "


示例

"""
{\n
  "ip": "50.198.81.174",\n
  "hostname": "50-198-81-174-static.hfc.comcastbusiness.net",\n
  "city": "Braintree",\n
  "region": "Massachusetts",\n
  "country": "US",\n
  "loc": "42.2038,-71.0022",\n
  "org": "AS7922 Comcast Cable Communications, Inc.",\n
  "postal": "02184"\n
}
"""

PHP中使用trim函数:

$trimmed = trim('"""" { ... } """', '"');
echo $trimmed;

输出:

{ ... }

您还可以使用 rtrim(从右侧删除)和 ltrim(从左侧删除)。