【问题标题】:PHP remove repeatedly remove lines after specific text to find XMLPHP删除重复删除特定文本后的行以查找XML
【发布时间】:2012-06-21 16:31:33
【问题描述】:

我有代码可以检索 stomp 消息,这很有效。然后我想从 stomp 消息中获取 xml 来做一些事情,我有代码并且它可以工作。

挑战是从消息中去除杂乱无章的内容,只获取 xml。

这是 stomp 消息的示例(是的,数据是相同的,但在这里不相关):

MESSAGE
_HQ_ORIG_ADDRESS:jms.queue.edu
timestamp:1339716293764
redelivered:false
_HQ_ORIG_MESSAGE_ID:xxxxxxxx
expires:0
subscription:subscription/jms.queue.edu
priority:4
message-id:xxxxxxxxxx
destination:jms.queue.edu

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>
MESSAGE
_HQ_ORIG_ADDRESS:jms.queue.edu
timestamp:1339716293764
redelivered:false
_HQ_ORIG_MESSAGE_ID:xxxxxxxx
expires:0
subscription:subscription/jms.queue.edu
priority:4
message-id:xxxxxxxxxx
destination:jms.queue.edu

<?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>

我想要做的是删除消息中从“MESSAGE”开始的所有行,包括以 xml 开头的每一行之前的换行符。这将为我提供使用 xml 解析器解析所需的结果:

    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?><create><sourceMessageId>4454</sourceMessageId><messageId>3038</messageId><course> <batchUid>ASIA.355.921.2012S1.6733</batchUid><title>ASIA355-921-Chinese Cinema</title><startDate>2012-06-18-07:00</startDate><endDate>2012-09- 21-07:00</endDate><mappedNodeBatchUid>9c0bc373-23a0-4e60-b201- efbbc9bb022e</mappedNodeBatchUid><available>false</available></course></create>

我试过了:

$xmlstr = preg_replace("/MESSAGE(.*)jms.queue.edu$/ims",'',$msg);
$xmlstr = trim($xmlstr);

但这会删除第一行第一次出现“MESSAGE”和最后一次出现 xml 之间的所有内容。换句话说,第一个“MESSAGE”和最后一个“xml”之间的所有行都被删除了。

有什么想法吗?我尝试过使用各种技巧,包括:正则表达式、内爆/爆炸、写入/读取文件等。但我觉得上面的 preg_replace 代码有效,它只需要能够识别所有事件。我知道这将涉及“while”或“foreach”循环,但我期待一个好的、干净的解决方案。非常感谢任何帮助。

【问题讨论】:

    标签: php xml stomp


    【解决方案1】:

    * 之后使用?

    或者,试试这个:

    list(,$body) = explode("\r\n\r\n",$msg); // adjust line ending as needed
    list($xmlstr) = explode("\r\n",$body);
    

    这将获得包含所有 XML 的行。

    【讨论】:

    • 谢谢。列表结果适用于获取第一个 xml,但不适用于其余部分 - $msg 中大约有 83 个条目: list(,$body) = explode("\n\n",$msg); list($xmlstr) = explode("\n",$body);
    • 并且 preg mod 工作但有第二个“MESSAGE”扭曲了结果:HQ_ORIG_MESSAGE_ID。 $xmlstr = preg_replace("/^MESSAGE(.*?)jms.queue.edu$/sm",'',$msg); $xmlstr = 修剪($xmlstr);
    • 已修复。这不是第二个 MESSAGE,而是第二个 jms.queue.edu。我更改为“/^MESSAGE(.*?)destination:jms.queue.edu$/sm”,它起作用了。感谢您的帮助!
    猜你喜欢
    • 2016-08-13
    • 1970-01-01
    • 2016-06-26
    • 1970-01-01
    • 2020-01-28
    • 2018-12-14
    • 2018-03-07
    • 2016-04-13
    • 1970-01-01
    相关资源
    最近更新 更多