PHP remove repeatedly remove lines after specific text to find XML

s2000

New Member
I have code to retrieve stomp messages, which works. I then want to grab the xml out of the stomp message to do stuff with, which I have code for and it works.The challenge is to strip out the cruft from the message and get only the xml.Here is a sample of the stomp message (yes the data is the same, but that's not relevant here):\[code\]MESSAGE_HQ_ORIG_ADDRESS:jms.queue.edutimestamp:1339716293764redelivered:false_HQ_ORIG_MESSAGE_ID:xxxxxxxxexpires:0subscription:subscription/jms.queue.edupriority:4message-id:xxxxxxxxxxdestination: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.edutimestamp:1339716293764redelivered:false_HQ_ORIG_MESSAGE_ID:xxxxxxxxexpires:0subscription:subscription/jms.queue.edupriority:4message-id:xxxxxxxxxxdestination: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>\[/code\]What I want to do is remove all the lines in the message starting from "MESSAGE" up to and including the line break before each line that starts with the xml. This will give me the results required to parse using an xml parser:\[code\] <?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>\[/code\]I tried:\[code\]$xmlstr = preg_replace("/MESSAGE(.*)jms.queue.edu$/ims",'',$msg);$xmlstr = trim($xmlstr);\[/code\]But that removes everything between the first occurrence of "MESSAGE" on the first line, and the last occurrence of the xml. In other words, all lines between the first "MESSAGE" and the last "xml" are removed.Any ideas? I've tried using a variety of tricks including; regex, implode/explode, writing/reading to a file, etc. But I feel the above preg_replace code works, it just needs to be able to recognize ALL occurrences. I know it will involve either a "while" or "foreach" loop, but I'm looking forward to a nice, clean solution. Any help is most appreciated.
 
Back
Top