$re = '/^.*<SRVCNAME>(?<SRVCNAME>[\/\w\?=]+)(<\/SRVCNAME>)?((<MESSAGETYPE>(?<MESSAGETYPE>.+?)<\/MESSAGETYPE>)?)?(<PROCESSORNAME>(?<PROCESSORNAME>.+)<\/PROCESSORNAME>)?(<CLNTIP>(?<CLNTIP>.+)<\/CLNTIP>)?(<TRANID>(?<TRANID>.+)<\/TRANID>)?(<TIMESTAMP>(?<TIMESTAMP>.+)<\/TIMESTAMP>)?(<TIMEELAPSED>(?<TIMEELAPSED>.+)<\/TIMEELAPSED>)?(<DOMAINNAME>(?<DOMAINNAME>.+)<\/DOMAINNAME>)?(<DEVICESERIALNUM>(?<DEVICESERIALNUM>.+)<\/DEVICESERIALNUM>)?((<INPUTMSG>)?(?<INPUTMSG>.*)(:?<\/INPUTMSG>.*))?(<\/CommonLoggingService>)?(?<line_breaker>(\<\?xml version="1\.0" encoding="UTF-8"\?>)?)?/m';
$str = '<CommonLoggingService><SRVCNAME>/apij/mme/telematics/GetAlertHistory</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.253:36682</CLNTIP><TRANID>477312335</TRANID><TIMESTAMP>2017-10-05T11:04:50-04:00</TIMESTAMP><TIMEELAPSED>360</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG>{"Data":{"Alerts":[],"SpeedAlerts":[]},"Response":{"ResponseCode":"1003","ResponseDescription":"No Records Found"}}</INPUTMSG></CommonLoggingService>
<CommonLoggingService><SRVCNAME>/apij/mme/aftersales/service/services/vmilite/4JGDA0EB4FA472807</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.250:36721</CLNTIP><TRANID>533680495</TRANID><TIMESTAMP>2017-10-05T11:05:12-04:00</TIMESTAMP><TIMEELAPSED>505</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803070</DEVICESERIALNUM><INPUTMSG>{"vin":"4JGDA0EB4FA472807","model":"ML250BTC","year":"2015","engineNo":"65196032390143","transmission":"72290810204344","prodDate":"08/29/2014","sellingDealer":"14146","primaryStatus":"NORMAL","warSale":{"wsStrDate":"10/30/2014"}}</INPUTMSG></CommonLoggingService>e><SRVCNAME>/apij/mme/telematics/QueryAccountDetails</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.252:36695</CLNTIP><TRANID>222825457</TRANID><TIMESTAMP>2017-10-05T11:05:12-04:00</TIMESTAMP><TIMEELAPSED>804</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG>{"context":{"SourceName":"WEB","TransactionId":"6K5U4YTE3023E1VDMW5Q8MU6ZT5S3R","Timestamp":"2017-10-05T15:05:10.583Z","Organization":"Mercedes-Benz","Region":"US","ApplicationName":"Android"},"Data":{"CustomerTier":"Customer","ESAFlag":"M","AccountName":"MBRACE FLEET","Address":{"AddressDetails":[{"Address1":"2002 SUMMIT BLVD","Address2":"","City":"BROOKHAVEN","StateProvince":"GA","Country":"USA","County":"GA","ZipCode":"30319-1560","AddressType":"Mailing Address","PrimaryAddressFlag":"Y"}]},"Phone":{"PrimaryPhone":{"PhoneNumber":"4047800377","CountryCode":""},"CellPhone":{"PhoneNumber":"","CountryCode":""},"HomePhone":{"PhoneNumber":"","CountryCode":""},"WorkPhone":{"PhoneNumber":"","CountryCode":""},"Fax":{"PhoneNumber":"","CountryCode":""},"PrimaryPhoneType":""},"EMail":{},"Contact":{"Name":{"FirstName":"MBRACE","MiddleName":"","LastName":"FLEET","Prefix":"","Suffix":"","PersonUId":"1-5346007"},"Address":{"AddressDetails":[{"Address1":"2002 SUMMIT BLVD","Address2":"","City":"BROOKHAVEN","StateProvince":"GA","Country":"USA","County":"","ZipCode":"30319-1560","AddressType":"Mailing Address","PrimaryAddressFlag":"Y","Longitude":"","Latitude":""}]},"Phone":{"PrimaryPhone":{"PhoneNumber":"4047800377","CountryCode":""},"CellPhone":{"PhoneNumber":"4047800377","CountryCode":""},"HomePhone":{"PhoneNumber":"","CountryCode":""},"WorkPhone":{"PhoneNumber":"4047800377","CountryCode":""},"Fax":{"PhoneNumber":"","CountryCode":""},"PrimaryPhoneType":"Mobile"},"Email":{"Email":[{"EmailAddress":"dele.nonyejohn@verizon.com"}]},"ContactType":"Primary","PreferredContactMethod":"Email Address","AlternateContactMethod":"Home Phone #","ServiceMSGContactMethod":"Email","ServiceMSGPhoneNumber":"Mobile Phone #","ServiceMSGWirelessCarrier":"","TimeZone":"","OptionalFlag":"N","PIN":"8744","Comments":""},"AccountNumber":"38184765","AccountType":"Subscriber","AccountStatus":"Active","PreferredDealer":"","AANFlag":"Y","SalesDealer":"","EmergencyFirstName":"","EmergencyLastName":"","EmergencyPhone":"","Relationship":""},"Response":{"ResponseCode":"00","ResponseStatus":"SUCCESS","ResponseDescription":"ACCOUNT INFORMATION FOUND."}}</INPUTMSG></CommonLoggingService><?xml version="1.0" encoding="UTF-8"?>
<CommonLoggingService><SRVCNAME>/apij/mme/customer/services/finAccountInfo/?vin=WDC0J4KB1HF232726</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.248:36776</CLNTIP><TRANID>533647919</TRANID><TIMESTAMP>2017-10-05T11:04:50-04:00</TIMESTAMP><TIMEELAPSED>220</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803070</DEVICESERIALNUM><INPUTMSG>
<CommonLoggingService><SRVCNAME>/apij/mme/aftersales/service/services/vmilite/WDC0G4JB0HV006098</SRVCNAME><MESSAGETYPE>request</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.251:33618</CLNTIP><TRANID>477313711</TRANID><TIMESTAMP>2017-10-05T11:04:50-04:00</TIMESTAMP><TIMEELAPSED>1</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG/></CommonLoggingService>e><?xml version="1.0" encoding="UTF-8"?>
<CommonLoggingService><SRVCNAME>/apij/mme/telematics/VehicleTra<?xml version="1.0" encoding="UTF-8"?>
<CommonLoggingService><SRVCNAME>/apij/mme/telematics/VehicleTrackingStatus</SRVCNAME><MESSAGETYPE>response</MESSAGETYPE><PROCESSORNAME>EAI-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.248:33990</CLNTIP><TRANID>221204193</TRANID><TIMESTAMP>2017-10-05T11:22:26-04:00</TIMESTAMP><TIMEELAPSED>377</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803070</DEVICESERIALNUM><INPUTMSG>{"context":{"SourceName":"WEB","TransactionId":"CGYWNXEGF9RDY7VDEJUYN8S2HESDAB","Timestamp":"2017-10-05T15:22:24.772Z","Organization":"Mercedes-Benz","Region":"US","ApplicationName":"Android"},"Data":{"trackingMode":"STOP","trackingInterval":0},"Response":{"ResponseCode":"2000","ResponseStatus":"Success","ResponseDescription":"Successful Execution"}}</INPUTMSG></CommonLoggingService>-ROUTER-MPG</PROCESSORNAME><CLNTIP>53.30.178.250:36775</CLNTIP><TRANID>223870097</TRANID><TIMESTAMP>2017-10-05T11:22:26-04:00</TIMESTAMP><TIMEELAPSED>995</TIMEELAPSED><DOMAINNAME>EAIProd</DOMAINNAME><DEVICESERIALNUM>7803112</DEVICESERIALNUM><INPUTMSG>{"Response":{"Tracing":{"TraceID":"JA6RH30553DV1O4WL2U638O4C2MVF3","TraceTimestamp":"2017-10-05T11:22:25.656Z"},"ResponseCode":"2000","ResponseStatus":"Success","ResponseDescription":"Successful Execution"}}</INPUTMSG></CommonLoggingService><?xml version="1.0" encoding="UTF-8"?>';
preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);
// Print the entire match result
var_dump($matches);
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for PHP, please visit: http://php.net/manual/en/ref.pcre.php