1

I am parsing email raw text bodies, and they have no HTML, so it's a multi line string essentially.

I thought I created a regex to catch: Quote: #403 I need that back as [Quote, 403] from the entirety of this large string.

So heres an example:

$body = "
Test Person
Tester
(123) 123-1234
 <mailto:[email protected]> [email protected]
[email protected]




Your Information:

Name: Last, First
Email: [email protected] <mailto:[email protected]> 
Phone: (123) 123-1234

Quote: #403";

preg_match('/^[a-zA-Z]+: #[0-9]+$/i', $body,$matches);

var_dump($matches); // => array(0) {}

Matches is empty. Based on my understanding this should have worked.

Thoughts?

3
  • Instead of /i try /m Commented Dec 13, 2021 at 21:12
  • Add the m flag for multiline: See demo Commented Dec 13, 2021 at 21:13
  • 1
    ^ asserts the beginning of the string, not the beginning of the line, unless you're using /m (multiline) Commented Dec 13, 2021 at 21:14

1 Answer 1

2

Try removing the "start" and "end" characters and make it multi-line:

preg_match('/[a-zA-Z]+: #[0-9]+/m', $body,$matches);
Sign up to request clarification or add additional context in comments.

4 Comments

This works and I technically can "explode" on the hash to get the quote number. I was just hoping to use regex to get it back in the array I wanted: [Quote, 403] but this works.
@TheWebs You can use capturing groups (demo)
@bobblebubble What would I do with out you :D Hahahaha thanks friend :D
: ) You welcome @TheWebs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.