Re: Parsing Subject and Ticket #
| From: | Christoph Becker | Date: | Sun, 09 Feb 2014 18:48:17 +0000 |
| Subject: | Re: Parsing Subject and Ticket # | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-322802@lists.php.net to get a copy of this message | ||
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