Discussion:
Parsing Subject and Ticket #
Ron Piggott
2014-02-09 16:32:17 UTC
Permalink
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
TR Shaw
2014-02-09 16:44:21 UTC
Permalink
Well I would have used pcre on the subject but since you start off parsing yourself...

$idx = strpos($subject, " [ Ticket ");
if ($idx) {
// Probably is my kind of title
$title = substr($subject, $idx + 1);
$ticket = substr($subject, $idx + 9, -2);
$subject_array = array( ‘subject’=>$title, ‘ticket_profile_reference’=>$ticket );
} else {
// Some other kind of subject
$subject_array = array();
}
Post by Ron Piggott
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.
$subject = 'Translation Idea [ Ticket 1005 ]';
$subject_array = array( ‘subject’=>’Translation Idea’ , ‘ticket_profile_reference’=>1005 );
$subject = 'Translation idea';
$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
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Giner
2014-02-09 16:55:04 UTC
Permalink
Post by Ron Piggott
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.
$subject = 'Translation Idea [ Ticket 1005 ]';
$subject_array = array( ‘subject’=>’Translation Idea’ , ‘ticket_profile_reference’=>1005 );
$subject = 'Translation idea';
$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
Perhaps if you broke down your logic into several statements until you
got it working?
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Ken Robinson
2014-02-09 18:04:29 UTC
Permalink
Post by Ron Piggott
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.
$subject = 'Translation Idea [ Ticket 1005 ]';
$subject_array = array('subject'=>'Translation Idea' ,
'ticket_profile_reference'=>1005 );
$subject = 'Translation idea';
$subject_array = array( 'subject'=>'Translation Idea',
'ticket_profile_reference'=>FALSE );
The ticket # will always be in the format: [ Ticket # ]
Don't over think this. It's probably best attacked with regex, but
since I'm weak at that, here's my take at it:

<?php
$subject = 'Translation Idea [ Ticket 1005 ]';
list($subject,$ticket_profile_reference) = explode(' [ ', $subject);
$ticket_profile_reference = ($ticket_profile_reference != '') ?
trim(ltrim(rtrim($ticket_profile_reference, ' ]'), 'Ticket')) : FALSE;
$subject_array = array('subject'=>$subject,
'ticket_profile_reference'=>$ticket_profile_reference);
var_export($subject_array);
?>

Ken
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Christoph Becker
2014-02-09 18:48:17 UTC
Permalink
Post by Ron Piggott
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.
$subject = 'Translation Idea [ Ticket 1005 ]';
$subject_array = array( ‘subject’=>’Translation Idea’ , ‘ticket_profile_reference’=>1005 );
$subject = 'Translation idea';
$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;
Post by Ron Piggott
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
Jim Lucas
2014-02-10 17:39:48 UTC
Permalink
Post by Ron Piggott
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.
$subject = 'Translation Idea [ Ticket 1005 ]';
$subject_array = array( ‘subject’=>’Translation Idea’ , ‘ticket_profile_reference’=>1005 );
$subject = 'Translation idea';
$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/***@actsministries.org/20140209.php
--
Jim Lucas

http://www.cmsws.com/
http://www.cmsws.com/examples/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Jim Lucas
2014-02-10 17:54:16 UTC
Permalink
Post by Jim Lucas
Post by Ron Piggott
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.
$subject = 'Translation Idea [ Ticket 1005 ]';
$subject_array = array( ‘subject’=>’Translation Idea’ ,
‘ticket_profile_reference’=>1005 );
$subject = 'Translation idea';
$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...
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/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Loading...