-1

What I’m trying to do seems like something that should be relatively simple.

I have a block of text that’s part of a message, and in it are the file paths of files that need to be attached to an outgoing email.

For example, a line in the message could be (brackets are part of the message text as well):

(File Attachment: /home/username/public_html/someFile.pdf)

There could be multiple files per message, but each would be listed on a separate line and each would begin with “File Attachment: “ and be enclosed in brackets.

I’m trying to figure out how to use regular expressions to:

  • check if the message contains the string “(File Attachment: .......)“
  • for each attachment, get the file name that’s listed as a PHP variable.
  • Remove the entire “File Attachment: “ line from the original message.

I know how to do everything else, it’s just this part I’m stuck on and could use a little help with because I don’t understand regular expressions at all.

A sample message would be something like this:

Application #123456 has been APPROVED!

(File Attachment: /home/username/public_html/someFile.pdf)
(File Attachment: /home/username/public_html/someFile2.pdf)

What I’d like is to get a PHP array with “ /home/username/public_html/someFile.pdf” and “ /home/username/public_html/someFile2.pdf”, and for those sub strings to be completely removed from the message.

Sherwin Flight
  • 2,174
  • 6
  • 29
  • 52

2 Answers2

0

This regular expression would work /\(File Attachment:\s(.*)?\)[\r\n]?/m you can see the details of why in this saved regex101 fiddle:

Or see a working PHP example here

<?php

$re = '/\(File Attachment:\s(.*)?\)[\r\n]?/m';
$str = 'Application #123456 has been APPROVED!

(File Attachment: /home/username/public_html/someFile.pdf)
(File Attachment: /home/username/public_html/someFile2.pdf)

dfsdfdsf
dsfdsfdsf

sdfdsfdsf
sdf';

preg_match_all($re, $str, $matches, PREG_SET_ORDER, 0);


$files = [];
foreach ($matches as $match) {
    $files[]= $match[1];
}

// remove potential duplicates
$files = array_unique($files);


$str = preg_replace($re, '', $str);



// Print the results
var_dump($files);
echo "\n\n";
echo $str;
Wesley Smith
  • 17,890
  • 15
  • 70
  • 121
  • That you so much. This did everything exactly as I needed it. You're the best. And I also appreciate the check for duplicate files as well. While that shouldn't happen, it's always great to have additional checks in place just in case. – Sherwin Flight Oct 15 '20 at 05:15
  • 1
    And thanks for the link to the example as well. I like the way it breaks it down with an explanation based on this specific example. Regular expressions are something I've always found confusing, so I'm going to use that website to help try to figure them out. (I also upvoted the comment on my question that suggested the same site) – Sherwin Flight Oct 15 '20 at 05:17
0
$file_string = "Application #123456 has been APPROVED!

(File Attachment: /home/username/public_html/someFile.pdf)
(File Attachment: /home/username/public_html/someFile2.pdf)";

// use preg_match_all to return all matches not just the first one
// we use the i flag to make the regex case insensitive
preg_match_all("/\(file attachment: (.*)\)/i", $file_string, $matches, PREG_SET_ORDER);

// loop through the resulting matches
foreach($matches as $match){
    // $match[0] = full match
    // $match[1] = first capture group
    
    $file_name = $match[1];
}
Rylee
  • 696
  • 2
  • 9