Search code examples
phppreg-match

How to get numbers from string


I am currently developing a script using imap to get emails and insert it onto mysql. basically a ticketing system.. The script works great and inserts data into MySQL but I am having some issues to update a current ticket. I would like to grab the ticket number from the email subject and update that ticket. below is what I have so far. any suggestions?

The subject looks like this: RE:Ticket#12345 - Testing Ticket creation. What I need to do is to get the numbers between # and -, match it against the database and update. Currently, it is just inserting new items into MySQL.

/* try to connect to the mailbox*/
    $inbox = imap_open($hostname,$username,$password) or die('Cannot connect: ' . imap_last_error());
}elseif($eml_service_control =="0"){
    echo 'Email to Tickets Service has been disabled by the system Administrator';
}

/* If all goes well then let us grab the emails */
$emails = imap_search($inbox,'UNSEEN');

/* if emails are returned, cycle through each... */
if($emails) {

    /* begin output var */
    $output = '';

    /* put the newest emails on top */ 
    rsort($emails);

    foreach($emails as $email_number){
        $overview = imap_fetch_overview($inbox,$email_number,0);
        $message = imap_fetchbody($inbox,$email_number,2);
        $seen=$overview[0]->seen;
        $date=$overview[0]->date;
        $date2=$overview[0]->udate;
        $from_email=imap_rfc822_parse_adrlist($overview[0] 
->from, 'localhost');$from_email=$from_email[0];
        $from_email=clean($from_email->mailbox."@".$from_email->host);
        $subject=clean($overview[0]->subject);
        $subject1=$overview[0]->subject;
        $preg=preg_match("/Ticket#ID - ([0-9]+\s)/b", $subject1, $res);
        $wo_id=extract_numbers($overview[0]->subject);print_r($res);
        $wo_id=$wo_id[0];
        $wo_id=$res[1];
        if(!$wo_id){ $wo_id="";}
        $message=strip_tags($message,"<br><div><p>");
        $message=clean("Received <b>$date</b><br/><hr/>$message");
        $q=mysql_query("SELECT * FROM `table_customer` WHERE
CUSTOMER_EMAIL='$from_email'");
        if(mysql_num_rows($q)==1){ $customer_id=mysql_fetch_array($q); 
$customer_id=$customer_id['CUSTOMER_ID']; } else { $customer_id=""; }
            print_r($overview);
            $q=mysql_query("SELECT * FROM tickets WHERE TICKET_ID='$ticket_id'") or die(mysql_error());
            $n=mysql_num_rows($q);
            $date2=date("H:i:s", $date2);
            $priority="1";
            if($n==0){
                mysql_query("INSERT INTO `tickets` 
                    (SCOPE, DESCRIPTION, PRIORITY_ID, CREATED_TIME, OPEN_DATE, STATUS, CUSTOMER_ID)
                    VALUES
                    ('$subject', '$message','$priority', '$date2', '$date', '0', '$customer_id')");
            } elseif($n==1) {
                mysql_query("UPDATE tickets SET DESCRIPTION = CONCAT
('Received <b>$date</b><br/><hr/>$message <hr/><hr/>', DESCRIPTION) WHERE 
TICKET_ID='$ticket_id'") or die(mysql_error());
            }
            $status = imap_setflag_full($inbox, $email_number, "\\Seen \\Flagged", ST_UID);
            imap_delete($inbox, $email_number);
        }
        echo $output;
    } else {
        echo "<!-- debug -->"; 
    }

    /* close the connection */
    imap_close($inbox);

Solution

  • preg_match("/Ticket#([0-9]+)/", $subject, $output);
    $output[1];
    

    Would return the number from any string that contains Ticket#{{number}}.