PHP removing brackets and name from email address

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

PHP removing brackets and name from email address

Post by cjkeane »

is there a way to strip the name, <> and [] from the below email address examples so that just the plain email address appears?

John <john@home.ca>
<john@home.ca>
[john@home.ca]
john smith [john@home.ca]

thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP removing brackets and name from email address

Post by requinix »

I'd go for regular expressions.

Code: Select all

/(?<=[<\[]).*?(?=[>\]]$)/
Feed that to preg_match like

Code: Select all

if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $email, $match)) {
    $email = $match[0];
}
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

sorry, but what does $match[0]; mean?

also i would need the name and the quotes removed if the email address appears like so:

"Smith, John" <John.Smith@home.ca>

the email address should just appear as: Johm.Smith@home.ca in any instance i mentioned above. is there a way to do that as well? Thanks.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP removing brackets and name from email address

Post by requinix »

$match is an array "returned" by preg_match() including things that were found during the regular expression search. [0] is the entire string matched, in this case just the email address.

As for the name, it doesn't matter if it's in quotes or whatever - all the expression wants is the <email> or [email].
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

i'm using the following code:

<textarea name="emailTo" cols="60" rows="2" readonly="readonly" id="emailTo">
<?php
$Sender = $row['sender'];
if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $Sender, $match)) {
$Sender = $match[0];}
echo safe($Sender) ?>
</textarea>

i'm still getting "john smith" <john.smith@home.ca>
i just would prefer it to display: john.smith@home.ca

any further ideas?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP removing brackets and name from email address

Post by requinix »

Are you sure the $Sender contains <>s and not the HTML entities < and >? That's the only thing I can think of.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: PHP removing brackets and name from email address

Post by mecha_godzilla »

Just to add to that, you won't know if the search string is being matched or not because the echo() code isn't inside the if() test - the best way to check whether the < > characters have been made safe with htmlspecialchars() is to echo out the email address to your browser and then view the page source.

HTH,

Mecha Godzilla
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

requinix wrote:Are you sure the $Sender contains <>s and not the HTML entities < and >? That's the only thing I can think of.
there are two fields actually i need this resolved on, the $Sender and the $toEmail variables.

you are correct. $Sender in the db it appears as: "Smith, John" <John.Smith@home.ca> so that's why that isnt displayed correctly on the page.
but the $toEmail in the db appears as: "john@home.ca" <john@home.ca> and on the page as:"john@home.ca" <john@home.ca>

using your suggestion, doesn't seem to work on either variable. hmm
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

mecha_godzilla wrote:Just to add to that, you won't know if the search string is being matched or not because the echo() code isn't inside the if() test - the best way to check whether the < > characters have been made safe with htmlspecialchars() is to echo out the email address to your browser and then view the page source.

HTH,

Mecha Godzilla
I moved the echo inside the If statement and then upon refreshing the page, the $Sender field appeared blank.
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: PHP removing brackets and name from email address

Post by mecha_godzilla »

That means that preg_match isn't matching your email address then, which suggests that the < > characters in your email address have already been converted to "safe" versions.

For testing purposes, have you tried hard-coding an email address into your script? The following code works fine for me

Code: Select all

$email = '"Smith, John" <John.Smith@home.ca>';
if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $email, $match)) {
    $safe_email = $match[0];
}
echo $safe_email . '<br />';
var_dump($match);
and this is the output in my browser window.

Code: Select all

John.Smith@home.ca
array(1) { [0]=> string(18) "John.Smith@home.ca" }
If your email address has already been made "safe" by another part of your script then it may look like this to the script

Code: Select all

$email = '"Smith, John" <John.Smith@home.ca>';
if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $email, $match)) {
    $safe_email = $match[0];
}
echo $safe_email . '<br />';
var_dump($match);
and give this output

Code: Select all


array(0) { } ]
HTH,

M_G
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

The email addresses are being retrieved from a db. my safe function, is as follows:

Code: Select all

function safe($var){ 
        $pattern = '/&(#)?[a-zA-Z0-9]{0,};/'; 
        if (is_array($var)) {    // If variable is an array 
            $out = array();      // Set output as an array 
            foreach ($var as $key => $v) {      
                $out[$key] = safe($v);         // Run formspecialchars on every element of the array and return the result. Also maintains the keys. 
            } 
        } else { 
            $out = $var; 
            while (preg_match($pattern,$out) > 0) { 
                $out = htmlspecialchars_decode($out,ENT_QUOTES);       
            }                             
            $out = htmlspecialchars((trim($out)), ENT_QUOTES,'UTF-8',true);     // Trim the variable, strip all slashes, and encode it 
            
        } 
        // Stripslashes

        return $out; 
    }  
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

mecha_godzilla wrote:That means that preg_match isn't matching your email address then, which suggests that the < > characters in your email address have already been converted to "safe" versions.

For testing purposes, have you tried hard-coding an email address into your script? The following code works fine for me

Code: Select all

$email = '"Smith, John" <John.Smith@home.ca>';
if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $email, $match)) {
    $safe_email = $match[0];
}
echo $safe_email . '<br />';
var_dump($match);
and this is the output in my browser window.

Code: Select all

John.Smith@home.ca
array(1) { [0]=> string(18) "John.Smith@home.ca" }
If your email address has already been made "safe" by another part of your script then it may look like this to the script

Code: Select all

$email = '"Smith, John" <John.Smith@home.ca>';
if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $email, $match)) {
    $safe_email = $match[0];
}
echo $safe_email . '<br />';
var_dump($match);
and give this output

Code: Select all


array(0) { } ]
HTH,

M_G
So what would you suggest in order for me to remove everything prior to and including < and > ?
User avatar
mecha_godzilla
Forum Contributor
Posts: 375
Joined: Wed Apr 14, 2010 4:45 pm
Location: UK

Re: PHP removing brackets and name from email address

Post by mecha_godzilla »

Assuming your email address (to PHP) looks like this

Code: Select all

$email = '"Smith, John" <John.Smith@home.ca>';
you could just do this

Code: Select all

$email = str_replace('<', '<', $email);
$email = str_replace('>', '>', $email);
and preg_match should be able to match it.

It sounds like your email addresses have already been saved in a "safe" format in the database, assuming that you haven't run them through your "safe" function prior to matching them with preg_match.

M_G

P.S. I'm sneaking off to bed now while this topic has temporarily gone quite... :mrgreen:
cjkeane
Forum Contributor
Posts: 217
Joined: Fri Jun 11, 2010 1:17 pm

Re: PHP removing brackets and name from email address

Post by cjkeane »

That seems to have done it for me.. at least displaying on the page, now only shows the email address which is what i wanted so thank you everyone for you input!

Correction: There is still one issue which still need to be resolved.

Retrieving the row source from the db displays the email the way I want, such as johnsmith@home.ca, however if there are multiple email addresses, they are retrieved like so: johnsmith@home.ca>, peter <peter@home.ca

I need it to display as johnsmith@home.ca, peter peter@home.ca (eliminating all brackets and display names)

Code: Select all

 <textarea name="sender" cols="60" rows="2" id="sender"><?php
        $sender = str_replace("\0",'',(fixUTF8($sender)));
		$senderfixed = safe($sender);
		$senderfixed = str_replace('<', '<', $senderfixed);
		$senderfixed = str_replace('>', '>', $senderfixed);
		
		if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $senderfixed, $match)) {
			$senderfixed = $match[0]; 
		echo $senderfixed;
		} else {
		 echo $senderfixed;
		}
		
		?>
        </textarea>
Any suggestions?
Post Reply