把一根绳子剪成一根绳子

发布于 2024-12-15 20:39:05 字数 1667 浏览 0 评论 0原文

我如何只从这个响应中获取肥皂信封:

Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><MResponse xmlns="http://xxx.gateway.abc.abcd.com"><return><transaction_id>123456</transaction_id><error_code>109</error_code><error_desc>Service is inactive (suspended/terminated)</error_desc><error_list>na</error_list><success_list></success_list></return></MResponse></soap:Body></soap:Envelope>
------=_Part_0_9361192.1321179416623--

当我使用下面的代码时...

$sope_responce_with_garbage = stristr($response,'<soap:Envelope');

我也得到了字符串的其余部分:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><MResponse xmlns="http://xxx.gateway.abc.abcd.com"><return><transaction_id>123456</transaction_id><error_code>109</error_code><error_desc>Service is inactive (suspended/terminated)</error_desc><error_list>na</error_list><success_list></success_list></return></MResponse></soap:Body></soap:Envelope>
    ------=_Part_0_9361192.1321179416623--

但我只需要 之间的字符串> 和 。 我怎样才能得到这个结果?提前致谢。

How do I only get the soap envelope from this response:

Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
Content-Transfer-Encoding: binary
Content-ID: <[email protected]>
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><MResponse xmlns="http://xxx.gateway.abc.abcd.com"><return><transaction_id>123456</transaction_id><error_code>109</error_code><error_desc>Service is inactive (suspended/terminated)</error_desc><error_list>na</error_list><success_list></success_list></return></MResponse></soap:Body></soap:Envelope>
------=_Part_0_9361192.1321179416623--

When I use the code below...

$sope_responce_with_garbage = stristr($response,'<soap:Envelope');

I get the rest of the string as well:

<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope"><soap:Body><MResponse xmlns="http://xxx.gateway.abc.abcd.com"><return><transaction_id>123456</transaction_id><error_code>109</error_code><error_desc>Service is inactive (suspended/terminated)</error_desc><error_list>na</error_list><success_list></success_list></return></MResponse></soap:Body></soap:Envelope>
    ------=_Part_0_9361192.1321179416623--

But I only need the string between <soap:Envelope> and </soap:Envelope>.
How can I get that result? Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

玩物 2024-12-22 20:39:06

您可以使用 strpos 查找任何字符串,并使用 substr

$upto = '';
$end = '</soap:Envelope>';
$r = strpos($subject, $end);
if ($r !== FALSE)
{
   $upto = substr($subject, 0, $r + strlen($end));
}

You can look-up any string with strpos and obtain a sub-string with substr:

$upto = '';
$end = '</soap:Envelope>';
$r = strpos($subject, $end);
if ($r !== FALSE)
{
   $upto = substr($subject, 0, $r + strlen($end));
}
风追烟花雨 2024-12-22 20:39:05

您可以使用正则表达式 (regex)。
正则表达式用于搜索和修改字符串。

在您的情况下,您需要函数: preg_match() ,它匹配模式并可选择将所有结果放入数组中。

$pattern = '/<soap:Envelope.*?>(.*)<\/soap:Envelope>/i'; // searches for your pattern
preg_match($pattern, $response, $matches);

然后,您的结果将保存在变量 $matches[0] 中!

You can use regular expressions (regex).
Regex are used to search and modify strings.

In your case you'd need the function: preg_match() which matches a pattern and optionally puts all results in an array.

$pattern = '/<soap:Envelope.*?>(.*)<\/soap:Envelope>/i'; // searches for your pattern
preg_match($pattern, $response, $matches);

Then, your result will be in the variable $matches[0]!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文