I am new in PHP. I am trying to extract subject and body text from the string like below
$string = "Subject:
Update on Your Ticket: ID {{ random_number }}
Body:
Hello,
Your ticket has been recently updated on {{ date }}. We kindly request you to review and acknowledge the changes made.
If you have any further updates or queries regarding your ticket, please don't hesitate to respond to this email.
Best Regards,
{{ sender_name }}";
What I want from it is like
$subject = "Update on Your Ticket: ID {{ random_number }}";
$body ="Hello,
Your ticket has been recently updated on {{ date }}. We kindly request you to review and acknowledge the changes made.
If you have any further updates or queries regarding your ticket, please don't hesitate to respond to this email.
Best Regards,
{{ sender_name }}"
I must clear that this is not static string, it will be dynamic string given by chat gpt. I have tried some questions and answers like
- Extract Text from a string in PHP
- How do I delete lines using PHP that do NOT contain a designated string?
But not getting any idea to solve puzzle from last 2 hours. Let me know if any one here can help me for solve the issue.
2
Answers
If you are sure that
Body:
will never be repeated, you can use simple functions to do this:Try it online
With regular expressions: