php - 如何从特定字符中剪切动态字符串并将剩余部分保留在变量中

标签 php string

我想要一个 json_decode 一个需要特定格式的字符串,但我的字符串包含一些额外的文本,所以我想剪切字符串中不固定的部分。我想从特定字符“{”中剪切。那么我怎样才能剪切该文本。

我尝试了这两个函数 substr() 和 strpos() 但这些函数返回我想要删除的字符串。从这里 PHP Cut String At specific character .

$responseArray = http_post_with_status_code($url, $post_string, "application/json", false, $options);

        $rawResponse = $responseArray['message'];

// This is the expected output of $rawResponse.

 $rawResponse = http code:200 Output:{
  "QueryResponse" : {
    "Header" : {
      "DateTime" : "2019-04-16T07:24:41.718Z",
      "MessageID" : "1111111111111111111111"
    },
  }
}

没有这些字符的字符串(http代码:200输出:)

最佳答案

我认为您正在寻找的功能是 strstr .

这应该可以解决问题:

strstr($rawResponse, '{');

关于php - 如何从特定字符中剪切动态字符串并将剩余部分保留在变量中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55758503/

相关文章:

php - 使用 Doctrine 2 和 Zend Framework 时业务逻辑应该放在哪里

php - 仅将默认数字关联数组键更改为 "number",而不影响数组值

java - (java.util.regex.Pattern 的问题)检查字符串中的数字和字母

json - Swift:从 NSURLSession 任务返回 JSON 字符串

python - 验证字符串不包含用于格式化的大括号

php - 配置 SAPRFC + PHP5 失败

PHP 部分缓存

php - 我在 Swift 中对 PHP 的 Json 请求出错

java - 用 Java 写入文本文件

c# - 将像 JSON 这样的字符串从 C# 发送到 javascript