Hey guys, I need to be able to get a certain value out of the following.

Job#    :   442254 

Contact :   TO BE ADVISED

Model   :   BZ500  50GN02630

Purchase Order/Your Reference : 

Contract Type:  C3  CopyPlan - Contract Minimum   
Job Type:  H  Audit, Admin, Housekeeping

Fault/Problem(s) reported: 
CHECK SENTINEL EMAIL/HTTPS

Solution: 
Install KRDS 6 via IP Kit

Job Notes:  
machine seems to be out of storage now but not on 
sentinel. 
please setup sentinel via email concentrator

The template is always the same in certain areas and the line I am interested in is

Model   :   BZ500  50GN02630

This 'Model :' is always consistent it's the 2 values after that change and it is the last value that I am after.

So will need a preg_match that finds the words Model : and looks for another that contains letters and numbers and can be between 2-10 chars and then looks for the last
value on the line and returns that value, the last value is letters and chars and can be 5-20 chars.

Cheers

I'm assuming you have the data in string format. If you do, this link is your new best friend. There are tons of string manipulation functions you can use and about 20 different ways to go about doing what you're describing. I would try to put something together then post if you have any issues.

http://php.net/manual/en/book.strings.php

Here's the basic order... more details in the link.


strpos — Find position of first occurrence of a string

substr — Return part of a string

trim — Strip whitespace (or other characters) from the beginning and end of a string

Model\s+:\s+(\w{2,10})\s+(\w{5,20})

\s allows whitespace
+ one or more
\w is a word character (letters, digit and underscore)
{2,10} between two and ten of the previous \w
() is a group that is returned separately

Model\s+:\s+(\w{2,10})\s+(\w{5,20})

\s allows whitespace
+ one or more
\w is a word character (letters, digit and underscore)
{2,10} between two and ten of the previous \w
() is a group that is returned separately

This is perfect, I got it working using the string functions post above yours but this is much more efficient, although I am struggling to work out how to get the last past

(\w{5,20})

to be returned as an array value, found a few examples that work on the net but unable to reverse this to get the same result, as it is the value in that last set of brackets I need to return.

Any pointers much appreciated.

<?php
  $subject = <<< END
Job#    :   442254 
 
Contact :   TO BE ADVISED
 
Model   :   BZ500  50GN02630
 
Purchase Order/Your Reference : 
 
Contract Type:  C3  CopyPlan - Contract Minimum   
Job Type:  H  Audit, Admin, Housekeeping
 
Fault/Problem(s) reported: 
CHECK SENTINEL EMAIL/HTTPS
 
Solution: 
Install KRDS 6 via IP Kit
 
Job Notes:  
machine seems to be out of storage now but not on 
sentinel. 
please setup sentinel via email concentrator
END;

  $pattern = '%Model\s+:\s+(\w{2,10})\s+(\w{5,20})%';
  preg_match($pattern, $subject, $matches);
  print_r($matches);
  echo '<hr/>';
  echo 'Model 1: ' . $matches[1] . '<br/>';
  echo 'Model 2: ' . $matches[2] . '<br/>';
?>

If this is not what you meant, please explain with a code sample.

Thanks for that, although it was exactly what I had, which means that the problem is how I get the data into my variable. Which is done via the imap functions

$imap = imap_open("{my server address:143}inbox/folder", "mailbox name", "password") or die ('No server for you: '.imap_last_error());
$i = 1;
$body = imap_body($imap,$i,FT_PEEK);  // this sets the string to check
$test = preg_match('%Model\s+:\s+(\w{2,10})\s+(\w{5,20})%', $body, $matches);
print_r($matches);
    echo 'Model 1: ' . $matches[1] . '<br/>';
    echo 'Model 2: ' . $matches[2] . '<br/>';
    if($test)
    {
      echo 'Match Found';
    }
    else
    {
      echo 'Next Time';
    }

this returns nothing and echos Next Time, If I add a [ ] to preg_match pattern it returns a; 'D' and Match Found.

If I put the text on the script page and assign it to variable then use the above code it works fine, so that problem must be in the fact i am using imap to read an emails body and then try process it.

Any ideas?

Is this a function that should return a value? Or do you want to store the number, so you can use it later ? If you can describe what you want to do with it, that will help. Is there more code following, if so, describe the logic flow.

The whole process is, we get an email from our printer maintenance company, I have written a script that goes to folders in the inbox and checks for unread emails, it takes the headers and body out and sends it via PHP to our job logging system. It logs a job, marks the email as read and then it needs the serial number to find the match in the DB and uses that match to get the email address of the person to forward the email to.

I have it working in that it gets the emails, logs the job and then marks it as read, I have the DB built and data entered. Now I just need a way of getting the serial number out of the email, it doesn't need to store the number, just needs it for that loop, as each loop represents an unread email that goes through the whole process, no more unread emails no more loops.

I have the code to send it to the DB and and do the check, , I have the serial number extraction working using the string functions suggested in post 2, but it takes about 3 inbuilt php functions to get the number, eg I find a starting point in the string, explode the string on white spaces, get the array position of the value I want and then trim the string.

Seems like a lot of work for a simple task, hence I thought the preg_match to get the serial number would be more efficient and it is but I can't seem to get it to work when using the data sucked from the email(via imap_body), however when its declared inside the script it works fine.

Are you sure $body is indeed a string, and not an array for example. I don't have access to IMAP so can't test this.

According to php.net http://www.php.net/manual/en/function.imap-body.php

the return of imap_body is: Returns the body of the specified message, as a string.

Anyway I just implemented it using string functions, bit more code but it works and means I can now continue on the PHP mail stuff, it works and the DB returns the right email address based on the serial number extracted.

Guess this stays as one of those annoying little things that you find workarounds for that does exactly the same job.

Cheers for all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.