2012-06-21 45 views
0

我有代码来检索stomp消息,它的工作原理。然后,我想从stomp消息中获取xml来完成与之相关的任务,这些代码都是针对代码进行的。PHP删除重复删除特定文本后的行以查找XML

挑战是从邮件中删除邮件并只获取xml。

这里是跺脚消息的样品(是的数据是一样的,但是这不是与此有关):

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); 

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

任何想法?我试过使用各种技巧,包括;正则表达式,implode/explode,写入/读取到文件等等。但是我觉得上面的preg_replace代码有效,它只需要能够识别所有出现的事件。我知道它会涉及“while”或“foreach”循环,但我期待着一个不错的,干净的解决方案。任何帮助最受赞赏。

回答

1

使用?*

另外,试试这个:

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

这将得到一个包含所有XML行。

+0

谢谢。列表结果用于抓取第一个xml,但不是其余的 - 在$ msg中有大约83个条目:list(,$ body)= explode(“\ n \ n”,$ msg); list($ xmlstr)= explode(“\ n”,$ body); – fowbar

+0

preg mod工作正常,但还有第二个“MESSAGE”会使结果偏斜:HQ_ORIG_MESSAGE_ID。 $ xmlstr = preg_replace(“/^MESSAGE(。*?)jms.queue.edu $/sm”,'',$ msg); $ xmlstr = trim($ xmlstr); – fowbar

+0

固定。这不是第二个MESSAGE,它是第二个jms.queue.edu。我更改为“/^MESSAGE(.*?)destination:jms.queue.edu$/sm”,它工作。谢谢你的帮助! – fowbar