2012-06-21 57 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