Forum Moderators: coopster

Message Too Old, No Replies

extract using search and match

         

gchristen

4:15 pm on Feb 25, 2010 (gmt 0)

10+ Year Member



Dear all.

I have a range of emails in the following format, which I open using PHP and assign it to attribute $email.

Number: 123456789 (line break)
Message: aaaaa bbbbb cccccc d eeeee ffffff ggggg hhhh etc

(bold mean text constant in each email)

I would somehow like to extract parts of these emails using PHP into two seperate attributes $number and $message.

In the example above, $number would be "123456789" (variable length) and $message "aaaaa bbbbb cccccc d eeeee ffffff ggggg hhhh etc" (also variable length).

is there a simple way to do this? I was thinking of using the search and match function but I am not sure how to continue from her.

Cheers.

Readie

4:32 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



So, you have one string that looks like this?:

$email_variable = 'Number: 123456789
Message: aaaaa bbbbb cccccc d eeeee ffffff ggggg hhhh etc';


If that's the case:

$mail_pieces = explode("Message: ", $email_variable);
$number_pieces = explode("Number: ", $mail_pieces[0]);
$number = rtrim($number_pieces[1]);
$message = $mail_pieces[1];


Should get you what you want.

EDIT:

Just a thought, the number seems to be pretty safe but if someone includes "Message: " in a message things could get hairy.

$mail_pieces = explode("Message: ", $email_variable);
$count = count($mail_pieces);
if($count == 2) {
$number_pieces = explode("Number: ", $mail_pieces[0]);
$number = rtrim($number_pieces[1]);
$message = $mail_pieces[1];
} else {
$number_pieces = explode("Number: ", $mail_pieces[0]);
$number = rtrim($number_pieces[1]);
$mail_pieces[0] = '';
for($i = 1; $i < $count; $i++){
$mail_pieces[$i] .= 'Message: ';
}
$message = array_merge($mail_pieces);
}


Bear in mind this code is typed on the fly and may contain an error or 2

gchristen

5:37 pm on Feb 25, 2010 (gmt 0)

10+ Year Member



Thank you Readie for your reply.

I am currently learning PHP from scratch and the examples in this forum are excellent learning resources.

As for the example below, I think I understand the simplified code. I really doubt that anyone would type in "message:space"into the email, so I will stick to the simple, 4-line code.

One thing I forgot to mention is that the number in the email is inside brackets, example: number: (12345678)

Once I have extracted (12345678) using your method, can I use the rtrim function to get rid of those brackets?

For example: $trimmednumber = rtrim (&number, "(", ")")

Or can I only trim of one character at a time? (in that case I would do this step twice)

again, thanks four your help

Readie

6:07 pm on Feb 25, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



rtrim = trim from the right of the string
ltrim = trim from the left of the string
trim = trim from both sides of the string

All three use the same syntax

[php.net...]

So, to do what you are asking:

$mail_pieces = explode("Message: ", $email_variable);
$number_pieces = explode("Number: ", $mail_pieces[0]);
$number = rtrim($number_pieces[1]);
$number = rtrim($number, ")");
$number = ltrim($number, "(");
$message = $mail_pieces[1];




Just as well you didn't use my second clump of code, I made a mistake in it:

for($i = 1; $i < $count; $i++){
$mail_pieces[$i] .= 'Message: ';
}


Should be

for($i = 0; $i < ($count - 1); $i++){
$mail_pieces[$i] .= 'Message: ';
}