Re: Parsing Subject and Ticket #

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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 # ]

I'd probably go with preg_match(). E.g. (error handling omitted):

  preg_match('/([^[]*)(?:\[ Ticket ([0-9]+) \])?/', $subject, $matches);
  $subject_array['subject'] = $matches[1];
  $subject_array['translation'] = isset($matches[2])
    ? $matches[2]
    : false;


> 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’.

It has nothing to do with the "simplified if" (aka. ternary
operator[1]), but rather with a superfluous semicolon and a missing
closing parentheses.  They following should work:

$subject_array['subject'] = trim( substr( $subject , 0 , ( strpos(
$subject , "[ Ticket " ) > 0 ) ? strpos( $subject , "[ Ticket " ) :
strlen( $subject ) ) );

However, keeping the expression simpler by splitting it across multiple
statements seems reasonable to avoid such kind of errors.

[1]
<http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary>

-- 
Christoph M. Becker

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux