I am using cakephp. And I have textarea field where users paste data, I using tinymce plugin to format text. I have warned users not to enter telephone number or email address inside the textarea. But, I dont want to take chances.
Is there a way I can extract the telephone number and email from textarea and replace it something like XXXX#gmail.com..
I appreciate any help.
Thanks.
Here's something off the top of my head for replacing the e-mail address with hidden:
$str = "My e-mail is shown#gmail.com Contact me for more details";
$str = preg_replace("/([a-zA-Z0-9\._]+)(#[a-zA-Z0-9\-\.]+)/", "hidden\\2", $str);
print($str);
The e-mail regex is not the best, but it's something that works for your example. You can get more interesting regexes (emails and phone numbers) at http://www.regexlib.com/ and use them with a simple preg_replace.
You could:
$string = "blabla#blablabla.com";
$parts = explode("#",$string);
\\$parts[0] contains the local part
\\$parts[1] contains the domain.
Keep in mind that, (even though it is not usual), the format defined by RFC 822 allows the "#" symbol to appear within quotation marks. This means: "bl#bla"#blablabla.com is technically correct.
Related
I want to validate email address and website in comment box. When someone writes comment in comment box and after submission check if email address or website found in comment remove that email and address.
I have put below regular expression for email.
"/(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")#(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/"
above expression validates email address but I want to validate like email[at]email[dot]com, email{at}email{dot}com, email(at)email(dot)com
Same for website validation I used below expression
"/((((http|https|ftp|ftps)\:\/\/)|www\.)[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,4}(\/\S*)?)/"
But I want to validate website like website[dot]com, www[dot]website[dot]com
Basically what you need to do is, where you have the validation of # and . character in email or . in weburl, you need to enhance your regex and put the alternatives to # character as you are expecting. So,
# should be written as (?:#|[[({]at[\]})])
And,
\. should be written as (?:\.|[[{(]dot[\]})])
wherever you have them in your regex and then it will also filter those strings as well.
Here is a modified regex for email.
(?:[a-z0-9!#$%&'*+=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+=?^_`{|}~-]+)*|\"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*\")(?:#|[[({]at[\]})])(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?(?:\.|[[{(]dot[\]})]))+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])
Regex Demo for email
Same way you can replace . from your website regex and your modified regex becomes this,
(?:(?:(?:http|https|ftp|ftps)\:\/\/)|www(?:\.|[[{(]dot[\]})]))(?:[a-zA-Z0-9.-]|[[{(]dot[\]})])+(?:\.|[[{(]dot[\]})])[a-zA-Z]{2,4}(\/\S*)?
Regex Demo for web url
Now besides matching of [dot], {dot} and (dot), the regex will also match [dot} and similar and as you are trying to detect such strings further, hence matching these strings will be an added advantage, rather than a problem unless the context was otherwise.
My message on from parameter comming with angles quotes, like that:
MyFromName <New message!>
I try $mail->ClearAllRecipients(), but not work.
Any tips?
Nope. You can't do this. It's part of the email specification, which says a mailbox (which is the thing you're talking about) is made up of:
mailbox = name-addr / addr-spec
name-addr = [display-name] angle-addr
angle-addr = [CFWS] "<" addr-spec ">" [CFWS] /
obs-angle-addr
That means an address can be in one of two forms when it appears in a header; as a name and address:
User Name <user#example.com>
Or as just an address:
user#example.com
Nearly everything uses the former, and usually if the name is given, that is displayed in preference to the address part, however, that is entirely up to the client application you're using to display the message, over which you have no control at all.
If you remove the angle brackets and keep the name, your message will never arrive because it's an invalid format.
You can use a replace function, for example;
function removeAngleBrackets($from)
{
// Create an array of things to replace
// Use both standard and html encoded to cover basis (you could use the ASCII too)
$replace = array("<", ">", "<", ">");
// Create the variable to replave the strings with
$replace_with = "";
// Replace this in the string and return the value when done
return str_replace($replace, $replace_with, $from);
}
Calling this on your string will remove the angle braces
oh eh...ya...lots commented there are lots email validation can be used but just that for this one I have to do it like what is mentioned below that's why....
I need to validate email like this
alphanumeric characters followed by # followed by alphanumeric characters followed by . followed by 2 – 4 more alphanumeric characters
this is what I have done but somehow I know it's the last part after . I messed up but I couldn't find where I messed up....
preg_match("/^([0-9]|[a-z])([0-9]|[a-z]|[_-])*#([0-9]|[a-z])*\.([0-9][a-z]){2,4}$/i","")
at start I used [0-9]|[a-z])([0-9]|[a-z]|[_-] because I didn't want people able to use _- as the start....so forced start as number/letters only
There must be a million different people that wrote a new regex for email validation. If you are interested in the email format you can just use
$email = filter_var($email, FILTER_VALIDATE_EMAIL);
and if the final value is empty the initial one wasn't a valid email address format.
(as an extra step you could try to validate the domain by using this function http://php.net/manual/en/function.checkdnsrr.php)
Have a try with this:
^[0-9a-z_\-]+#[0-9a-z_\-]+\.[0-9a-z]{2,4}$
But as said: there are ready-to-use regexes, much better than trying to reinvent the wheel. Also this current approach does not macth all valid addresses and validates some addresses that are illegal.
Which reason of email validation? It is very upset when you try to enter you email and you can't due to the stupid validation. I think it is enoth to check the availability of '#' and '.' signs, in case user unintentionally missed this.
$res = preg_match("/#[^#\.]*\./", $str);
I have an email address param where email addresses are passed un-encoded like so:
http://domain/script?email=test+test#gmail.com
What PHP escaping/encoding will let me safely display the email address on an input field on the page?
Everything I tried causes the encoded chars to show up instead of the user-friendly email address (i.e. test%2Btest%40test.com)
Update - here's what I've tried:
Going from ?email=test+test#gmail.com to:
urlencode($_GET['email']) = test+test%40test.com (# sign is encoded)
htmlspecialchars($_GET['email']) = test test#test.com (lost the +)
htmlspecialchars(urlencode($_GET['email']) = test+test%40test.com (# sign encoded)
Recall that I'm trying to take the unencoded url email param and safely output it into the value of an input field while keeping plus signs intact.
Maybe I should try this?
str_replace("%40", "#", htmlspecialchars(urlencode($_GET['email'])))
If you want to safely output it in the value of an input field, you need to htmlencode it first with htmlspecialchars.
Example :
<input type="email" name="email" value="<?php echo htmlspecialchars($_GET['email']); ?>"
Note : If you aren't using double quote around what you are output, you need to apply more escaping. This page explains it all.
This works:
str_replace("%40", "#", htmlspecialchars(urlencode($_GET['email'])))
You're probably looking for urldecode()? That's what converts %40, %2B, etc. back into normal characters.
use emailaddress = urldecode($_GET['email']); as Kevin suggested. it will do whatever you need.
What if you validate the email and write it as it was, if only an email address is acceptable?
From the following prose, I want to extract a list of numbers from any -line- that contains the letters "HTML". html could be upper case or lower case.
So here's the psuedo code:
text = getline()
if text contains html
match any numbers from text
return array of matches
Any ideas how to do this in REG Ex?
===============
HTML email is still … a great marketing tool if used properly. The key is to test, test,
test to see if your subscribers prefer 5 it over text based email. If you are unsure your
subscribers can read HTML email, then offer both text-based email and HTML 7 email, to
cater to both audiences.
In my Part $254,000 of this article, I will discuss “How to create and send an HTML email
form” to increase the interactivity of your subscribers and boost the response rate in your
email marketing campaigns. retro 50's
=============
First check if there is html in the string, then match all digits:
if (preg_match("/html/i", $input)) {
preg_match_all("/\b(\d+)\b/", $input, $m);
}
print_r($m);
Sample solution:
$line=strtolower($line);
$x = preg_match("/html.*(\d+)/",$line,$match) || preg_match("/(\d+).*html/",$line,$match);
if($x)echo "number in line: ".$match[1];
Assumes only one number in line. Definitely not the best way to do it, but you really should learn regex yourself - it's not that hard.
See also http://www.regular-expressions.info/
^((\d+)|.)*(HTML|html)((\d+)|.)*$
Getting the capture groups right might be a little tricky, but just mess around a little.