Jump to content

[SOLVED] Regex validation


stuffradio

Recommended Posts

I am working on some email thing.

 

I'm parsing an email message, and here is what I want to do but don't know how to do it.

 

The example message coming in should be formatted something like this:

Subject: <username>
Message:
Username: stuffradio
password: plaintext

Photo caption: Cool stuff!
<embedded photo>

The embedded photo represents the file of the photo being uploaded. I already figured out how to strip the photo from the email and upload it.

 

The "<username>" will be their username in the database. I want to take the argument of the Username which is stuffradio, and password which is plaintext and check the database.

 

The password will be encrypted when checking against the database.

 

Does anyone have any tips on what I need to do here?

 

Link to comment
https://forums.phpfreaks.com/topic/117996-solved-regex-validation/
Share on other sites

Probably other ways to do it, but here is a solutions. I am not very good with Regular Expressions. :P

 

<?php

$str = <<<html
Subject: <username>
Message:
username: stuffradio
password: plaintext

Photo caption: Cool stuff!
<embedded photo>
html;

preg_match("#username: ([a-zA-Z0-9]+)#i",$str,$username);
$username = $username[1];
preg_match("#password: ([a-zA-Z0-9]+)#i",$str,$password);
$password = $password[1];

echo "Username is {$username} and password is {$password}.";

?>

NOES! Do it in one regex call!

 

<?php

$subject = 'Subject: <username>
Message:
Username: stuffradio
password: plaintext

Photo caption: Cool stuff!
<embedded photo>';

preg_match_all( '/([a-z]++): (.++)/i', $subject, $matches, PREG_SET_ORDER );

# $matches[1][2] contains stuffradio
# $matches[2][2] contains plaintext
print_r( $matches );

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.