$re = '/^NoticeText:(?:(?!\nNoticeText:).)*\n\s+EventInfo(?:(?!\nNoticeText:).)*
\n\s+Text\s*\[str\]\s*=\s*"([^"]*)"(?:(?!\nNoticeText:).)*\nNoticeText:(?:(?!\nNoticeText:).)*\n\s+EventInfo(?:(?!\nNoticeText:).)*
\n\s+Text\s*\[str\]\s*=\s*"([^"]*)"(?:(?!\nNoticeText:).)*/six';
$str = 'NoticeText:
NoticeType [str] = USER_TYPING_ON
Text [str] = "user is typing"
EventInfo:
PartyId [int] = 2
EventType [str] = MESSAGE
UserNickname [str] = "Michael"
EventId [int] = 4
Text [str] = "Hey, how are you?"
MsgCheck [str] = NONE
TimeOffset [int] = 23
UserType [str] = AGENT
NoticeText:
NoticeType [str] = USER_TYPING_ON
EventInfo:
PartyId [int] = 1
EventType [str] = MESSAGE
UserNickname [str] = "Bob Smith"
EventId [int] = 6
Text [str] = "I\'m good, how are you?"
MsgCheck [str] = NONE
TimeOffset [int] = 28
UserType [str] = CLIENT
MessageType [str] = "text"';
$subst = "$1";
$result = preg_replace($re, $subst, $str);
echo "The result of the substitution is ".$result;
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