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

时间:2015-10-06 18:03:49

标签: php trim double-quotes

我有一个错误的数据"""" { ... } """ 我想稍微改进一下。

如何在开放"之前取消{,并在结束"后取消}

示例

"""
{\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
}
"""

1 个答案:

答案 0 :(得分:2)

PHP中使用trim功能:

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

<强>输出:

  

{...}

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