Re: Parsing Subject and Ticket #

From: Date: Mon, 10 Feb 2014 17:54:16 +0000
Subject: Re: Parsing Subject and Ticket #
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-322809@lists.php.net to get a copy of this message
On 02/10/2014 09:39 AM, Jim Lucas wrote:
On 02/09/2014 08:32 AM, Ron Piggott wrote:
I am trying to parse an e-mail subject so it returns the subject and also the ticket # *if* this is a reply to an existing ticket. For this subject:
    $subject = 'Translation Idea [ Ticket 1005 ]';
my desired result is:
    $subject_array = array( ‘subject’=>’Translation Idea’ ,
‘ticket_profile_reference’=>1005 ); And for this subject:
    $subject = 'Translation idea';
my desired result is:
     $subject_array = array( ‘subject’=>’Translation Idea’ ,
‘ticket_profile_reference’=>FALSE ); The ticket # will always be in the format: [ Ticket # ] The following is what I have tried so far. The first line of code is returning a syntax error. Apparently it doesn’t like the simplified ‘if’. I am asking for help to see how others would do this. <?php
     $subject_array['subject'] = trim( substr( $subject , 0 , ( strpos(
$subject , "[ Ticket " ) > 0 ) ? strpos( $subject , "[ Ticket " ) : strlen( $subject ); );
     $ticket_profile_reference = (int)trim( substr( $subject , ( strpos(
$subject , "[ Ticket " ) + 9 ) , ( strpos( $subject , " ]" ) - ( strpos( $subject , "[ Ticket " ) + 9 ) ) ) );
     if ( $ticket_profile_reference == 0 ) {
         $subject_array['ticket_profile_reference'] = FALSE;
     } else {
         $subject_array['ticket_profile_reference'] =
$ticket_profile_reference;
     }
     print_r( $subject_array );
?> Ron Piggott www.TheVerseOfTheDay.info
Gotta love regular expressions... http://www.cmsws.com/examples/php/testscripts/ron.piggott@actsministries.org/20140209.php
For archive purposes, here is the code: <?php echo '<pre>'; $subjects[] = 'Translation Idea [ Ticket 1005 ]'; $subjects[] = 'Translation Idea'; foreach ( $subjects AS $subject ) { preg_match('@^(?<subject>[\w\s]+)( \[ Ticket (?<ticket_profile_reference>[\d]+) \])?$@' ,$subject , $matches); $matches['ticket_profile_reference'] = (isset($matches['ticket_profile_reference'])?$matches['ticket_profile_reference']:false); var_export($matches); } -- Jim Lucas http://www.cmsws.com/ http://www.cmsws.com/examples/

« previous php.general (#322809) next »