PHP removing brackets and name from email address
Moderator: General Moderators
PHP removing brackets and name from email address
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.
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
I'd go for regular expressions.
Feed that to preg_match like
Code: Select all
/(?<=[<\[]).*?(?=[>\]]$)/Code: Select all
if (preg_match('/(?<=[<\[]).*?(?=[>\]]$)/', $email, $match)) {
$email = $match[0];
}Re: PHP removing brackets and name from email address
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.
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
$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].
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
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?
<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
Are you sure the $Sender contains <>s and not the HTML entities < and >? That's the only thing I can think of.
- 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
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
HTH,
Mecha Godzilla
Re: PHP removing brackets and name from email address
there are two fields actually i need this resolved on, the $Sender and the $toEmail variables.requinix wrote:Are you sure the $Sender contains <>s and not the HTML entities < and >? That's the only thing I can think of.
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
I moved the echo inside the If statement and then upon refreshing the page, the $Sender field appeared blank.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
- 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
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
and this is the output in my browser window.
If your email address has already been made "safe" by another part of your script then it may look like this to the script
and give this output
HTH,
M_G
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);Code: Select all
John.Smith@home.ca
array(1) { [0]=> string(18) "John.Smith@home.ca" }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);Code: Select all
array(0) { } ]M_G
Re: PHP removing brackets and name from email address
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
So what would you suggest in order for me to remove everything prior to and including < and > ?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
and this is the output in my browser window.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);
If your email address has already been made "safe" by another part of your script then it may look like this to the scriptCode: Select all
John.Smith@home.ca array(1) { [0]=> string(18) "John.Smith@home.ca" }
and give this outputCode: 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);
HTH,Code: Select all
array(0) { } ]
M_G
- 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
Assuming your email address (to PHP) looks like this
you could just do this
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...
Code: Select all
$email = '"Smith, John" <John.Smith@home.ca>';Code: Select all
$email = str_replace('<', '<', $email);
$email = str_replace('>', '>', $email);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...
Re: PHP removing brackets and name from email address
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)
Any suggestions?
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>