将字符串剪切为字符串

时间:2011-11-14 12:19:46

标签: php soap

我如何才能从此回复中获取肥皂信封:

Content-Type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
Content-Transfer-Encoding: binary
Content-ID: <root.message@cxf.apache.org>
<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--

但我只需要<soap:Envelope></soap:Envelope>之间的字符串。 我怎样才能得到这个结果?提前谢谢。

2 个答案:

答案 0 :(得分:1)

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

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

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

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

答案 1 :(得分:0)

您可以使用strpos查找任何字符串,并获取包含substr的子字符串:

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