Page 1 of 1

PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 4:30 pm
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.

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 4:44 pm
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];
}

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 4:46 pm
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.

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 5:24 pm
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].

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 5:28 pm
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?

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 5:57 pm
by requinix
Are you sure the $Sender contains <>s and not the HTML entities < and >? That's the only thing I can think of.

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 6:08 pm
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

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 6:17 pm
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

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 6:20 pm
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.

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 6:47 pm
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

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 6:54 pm
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; 
    }  

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 6:59 pm
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 > ?

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 7:06 pm
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:

Re: PHP removing brackets and name from email address

Posted: Thu Mar 07, 2013 7:39 pm
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?