[SOLVED] Need some guidance on extracting an email address from a string. |
5/5/2022 8:45:32 PM |
PHPRunner General questions | |
D
DealerModules authorDevClub member
Looking to extract email address from a field that is a string. The field named body contains the body of an email message. I would like to extract the TSMITH@TOMSREPAIR.COM and insert into a field name updated_email_address. Please fulfill this new order # 100088980 for: TOM SMITH Order Id:104581 Any direction is greatly appreciated. I have also read in the Email Reader add on: // your code here } Thanks in advance if anyone has some suggestions. |
|
![]() |
Admin 5/6/2022 |
The common approach is to use regex (regular expressions) for this kind of tasks. If we Google "regex extract text after word" it will point you to this article on StackOverflow. Now we can use an online regex tester to find out the correct regex string. We recommend using regex101.com . Paste your text in the TEST STRING window, paste the regex from the article to REGULAR EXPRESSION window, change the prefix to "Email: " and voila, it works.
Now you can even use their code generator to product the code in PHP. Of course, you need to change the hardcoded value of your email text to a variable. This is pretty much it. $re = '/(?<=Email: ).*?(?=[\s])/m'; |
D
|
DealerModules authorDevClub member 5/6/2022 |
Thanks Admin for the detailed explanation! I appreciate your help. Paul |
![]() |
Dalkeith 5/9/2022 |
Hi Paul Not sure if you are using SQL Server or not but you can generally do this in the database as well. I've had some success where I pull a text body into a text field and then call a user defined function from phprunner pulling the email addresses out. Here is a blog post I wrote to make sure that I don't forget how to do it which includes the userdefined function. SQL Azure - TSQL User Defined Funciton to separate multiple emails from nvarchar(max) field. and here is a link to regex alternate examples for Admin's Response |