I want to make sure the link is from the chosen social type.
help!!
$socialType = 'youtube';
$link = 'https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M';
if (!preg_match("/^(http|https):\\/\\/[a-z0-9_]+$socialType*\\.[_a-z]{2,5}"."((:[0-9]{1,5})?\\/.*)?$/i",$link))
{
return Response::json('inValid');
}
{
return Response::json('Valid');
}
There will be two options as-
1. with preg_match -
$subject = "https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M";
$pattern = '/^youtube/';
preg_match($pattern, substr($subject,7), $matches);
print_r($matches);
2. with strops as (Ruslan Osmanov)-
$socialType = 'youtube';
$link = 'https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M';
if (strpos($link, $socialType) !== false) {
return Response::json('Valid');
}
You can simply check, if the link contains your substring using strpos:
$link = 'https://www.youtube.co.uk/watch?v=DBK-Cy9ge4M';
$type = 'youtube';
if (strpos($link, $type) !== false) {
// passed
}
Or use a simple regular expression, if you want stricter check:
$reg_type = preg_quote($type, '/');
if (preg_match("/^https?:\/\/(www\.)?$reg_type/", $link)) {
// passed
}
Note, you should escape values passed into the regular expression using preg_quote.
The pattern should be just enough. Don't overcomplicate. It's generally impossible to write a perfect regular expression. For example, it is very unlikely to find HTTP(S) protocol prefix + optional "www." + "youtube." in an URL not belonging to Youtube.
Also, I wouldn't expect to get the answer with a universal regular expression for all kinds of social networks. Each has its own pattern.
Good evening.
I'm making an IRC bot that responds when you mention him. What I want to know is how to make him reply when someone actually says his name. This is what I have so far ($match[3] is the message that someone said on a channel and yes, stripos is because I want it case-insensitive ):
if (stripos($match[3], "ircBot") !== false) {
$isMentioned = true;
}else { $isMentioned = false; }
while this does in fact detect if someone said his name, it only works if he's mentioned at the very beginning of the message so for example:
"ircBot is at the beginning of this sentance" would make $isMentioned true
"There's ircBot in between this sentance" would make $isMentioned false
"At the end of this sentance is ircBot" would make $isMentioned false
I want it to return true if "ircBot" is anywhere inside $match[3] and not just the beginning
You have to look for word boundaries to avoid someone called MircBot
// using in_array
$isMentioned = in_array('ircbot', preg_split('/\s+/', mb_strtolower($match[3])));
// using regex word boundaries
$isMentioned = preg_match('/\b(ircBot)\b/i', $match[3]);
http://3v4l.org/lh3JT
Use stristr instead
if (stristr($match[3], "ircBot") !== false) {
$isMentioned = true;
}else { $isMentioned = false; }
I think your error is somewhere else, e.g. the construction of $match[3]. This works fine:
$isMentioned = stripos('This is in the middle of ircBot the string','ircbot') !== false;
echo( $isMentioned ? 'Is Mentioned' : 'Sad ignored bot');
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: preg_match() [function.preg-match]: Unknown modifier '/'
I am having troubles with this code wich gives me the next error:
Warning: preg_match() [function.preg-match]: Unknown modifier '{' in /usr/home/anubis-cosmetics.com/web/includes/functions/functions_email.php on line 568
Warning: preg_match() [function.preg-match]: No ending delimiter '^' found in /usr/home/anubis-cosmetics.com/web/includes/functions/functions_email.php on line 586
Line 568:
if email domain part is an IP address, check each part for a value under 256
if (!preg_match ($valid_ip_form, $domain)) {
$digit = explode( ".", $domain );
for($i=0; $i<4; $i++) {
if ($digit[$i] > 255) {
$valid_address = false;
return $valid_address;
exit;
}
Line 586:
if (!preg_match($space_check, $email)) { // trap for spaces in
if (!preg_match ($valid_email_pattern, $email)) { // validate against valid email patterns
$valid_address = true;
} else {
$valid_address = false;
return $valid_address;
exit;
}
}
return $valid_address;
}
?>
I don't know how to do it, can someone help me with this?
EDIT/////
I've tried to change the delimiters for this: # --->
// if email domain part is an IP address, check each part for a value under 256
if (!preg_match ($valid_ip_form, $domain))#
$digit = explode( ".", $domain );
And the other one for this:
if (!preg_match($space_check, $email)) { // trap for spaces in
if (!preg_match ($valid_email_pattern, $email)) { // validate against valid email patterns
$valid_address = true;
} else {
$valid_address = false;
return $valid_address;
exit;
}
}
return $valid_address;#
And still without work... Can someone be more especific (showing me some example inline) about the problem? Thanks!
The problem is with your $valid_ip_form respectively $space_check. They will be invalid php PCRE expressions.
I guess you're missing opening and closing delimiters and your expression looks like:
^[0-9]{3}$
Instead of:
~^[0-9]{3}$~
/^[0-9]{3}$/
#^[0-9]{3}$#
or whatever else you like.
Post them if you want more info
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
regular expression for letters, numbers and - _
I am creating a signup form in PHP and need to ensure that user's usernames don't contain unwanted characters. Is there anyway I can create a function that returns true for A-Z a-z 0-9 - . _.
Also I do not want the user's emails to be from yahoo as for some reason they reject the confirmation emails sent. Along with __FILTER_VALIDATE_EMAIL__what would I need to add?
PS: is there anything wrong with the characters I have mentioned above? I have noted that gmail doesn't allow -_ only . and YouTube only alpha-numeric characters.
Edited to use \w instead of a-zA-Z0-9_
if(preg_match("/[^\w-.]/", $user)){
// invalid character
}
if(!filter_var($email, FILTER_VALIDATE_EMAIL) || strstr($email,'#yahoo.com')) {
// either invalid email, or it contains in #yahoo.com
}
if(preg_match("/[^-A-Za-z0-9._ ]/", $userName)){
// there are one or more of the forbidden characters (the set of which is unknown)
}
<?php
// The validator class
class Validator
{
public function isValidUsername($username)
{
if(preg_match('/^[a-zA-Z0-9_\-\.]+$/', $username)) {
return true;
}
return false;
}
public function isYahooMail($mail) {
if(preg_match('/^[a-zA-Z0-9_\-\.]+#yahoo.com$/', $mail)) {
return true;
}
return false;
}
}
// The way to use this class
$username = "otporan_123";
$email = "otporan#gmail.com";
$badUsername = "otporan*bad";
$yahooEmail = "otporan#yahoo.com";
$validator = new Validator();
var_export($validator->isValidUsername($username));
echo "<br />";
var_export($validator->isValidUsername($badUsername));
echo "<br />";
var_export($validator->isYahooMail($email));
echo "<br />";
var_export($validator->isYahooMail($yahooEmail));
echo "<br />";
?>
This code would return:
true
false
false
true
This is a class, but you can see whats going on in methods and write your own functions if you like procedural code :)
Hope this helps!
if (!preg_match('/\w\-/', $username) {
//throw error
}
How can I validate the input value is a valid email address using php5. Now I am using this code
function isValidEmail($email){
$pattern = "^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$";
if (eregi($pattern, $email)){
return true;
}
else {
return false;
}
}
but it shows deprecated error. How can I fix this issue. Please help me.
You can use the filter_var() function, which gives you a lot of handy validation and sanitization options.
filter_var($email, FILTER_VALIDATE_EMAIL)
PHP Manual filter_var()
Available in PHP >= 5.2.0
If you don't want to change your code that relied on your function, just do:
function isValidEmail($email){
return filter_var($email, FILTER_VALIDATE_EMAIL) !== false;
}
Note: For other uses (where you need Regex), the deprecated ereg function family (POSIX Regex Functions) should be replaced by the preg family (PCRE Regex Functions). There are a small amount of differences, reading the Manual should suffice.
Update 1: As pointed out by #binaryLV:
PHP 5.3.3 and 5.2.14 had a bug related to
FILTER_VALIDATE_EMAIL, which resulted in segfault when validating
large values. Simple and safe workaround for this is using strlen()
before filter_var(). I'm not sure about 5.3.4 final, but it is
written that some 5.3.4-snapshot versions also were affected.
This bug has already been fixed.
Update 2: This method will of course validate bazmega#kapa as a valid email address, because in fact it is a valid email address. But most of the time on the Internet, you also want the email address to have a TLD: bazmega#kapa.com. As suggested in this blog post (link posted by #Istiaque Ahmed), you can augment filter_var() with a regex that will check for the existence of a dot in the domain part (will not check for a valid TLD though):
function isValidEmail($email) {
return filter_var($email, FILTER_VALIDATE_EMAIL)
&& preg_match('/#.+\./', $email);
}
As #Eliseo Ocampos pointed out, this problem only exists before PHP 5.3, in that version they changed the regex and now it does this check, so you do not have to.
See the notes at http://www.php.net/manual/en/function.ereg.php:
Note:
As of PHP 5.3.0, the regex extension is deprecated in favor of
the PCRE extension. Calling this
function will issue an E_DEPRECATED
notice. See the list of differences
for help on converting to PCRE.
Note:
preg_match(), which uses a Perl-compatible regular expression
syntax, is often a faster alternative
to ereg().
This is old post but I will share one my solution because noone mention here one problem before.
New email address can contain UTF-8 characters or special domain names like .live, .news etc.
Also I find that some email address can be on Cyrilic and on all cases standard regex or filter_var() will fail.
That's why I made an solution for it:
function valid_email($email)
{
if(is_array($email) || is_numeric($email) || is_bool($email) || is_float($email) || is_file($email) || is_dir($email) || is_int($email))
return false;
else
{
$email=trim(strtolower($email));
if(filter_var($email, FILTER_VALIDATE_EMAIL)!==false) return $email;
else
{
$pattern = '/^(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){255,})(?!(?:(?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)|(?:\\x22?[^\\x5C\\x22]\\x22?)){65,}#)(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22))(?:\\.(?:(?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)|(?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)))*#(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-+[a-z0-9]+)*\\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-+[a-z0-9]+)*)|(?:\\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\\]))$/iD';
return (preg_match($pattern, $email) === 1) ? $email : false;
}
}
}
This function work perfectly for all cases and email formats.
I always use this:
function validEmail($email){
// First, we check that there's one # symbol, and that the lengths are right
if (!preg_match("/^[^#]{1,64}#[^#]{1,255}$/", $email)) {
// Email invalid because wrong number of characters in one section, or wrong number of # symbols.
return false;
}
// Split it into sections to make life easier
$email_array = explode("#", $email);
$local_array = explode(".", $email_array[0]);
for ($i = 0; $i < sizeof($local_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9!#$%&'*+\/=?^_`{|}~-][A-Za-z0-9!#$%&'*+\/=?^_`{|}~\.-]{0,63})|(\"[^(\\|\")]{0,62}\"))$/", $local_array[$i])) {
return false;
}
}
if (!preg_match("/^\[?[0-9\.]+\]?$/", $email_array[1])) { // Check if domain is IP. If not, it should be valid domain name
$domain_array = explode(".", $email_array[1]);
if (sizeof($domain_array) < 2) {
return false; // Not enough parts to domain
}
for ($i = 0; $i < sizeof($domain_array); $i++) {
if (!preg_match("/^(([A-Za-z0-9][A-Za-z0-9-]{0,61}[A-Za-z0-9])|([A-Za-z0-9]+))$/", $domain_array[$i])) {
return false;
}
}
}
return true;
}
User data is very important for a good developer, so don't ask again
and again for same data, use some logic to correct some basic error in data.
Before validation of Email: First you have to remove all illegal characters from email.
//This will Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
after that validate your email address using this filter_var() function.
filter_var($email, FILTER_VALIDATE_EMAIL)) // To Validate the email
For e.g.
<?php
$email = "john.doe#example.com";
// Remove all illegal characters from email
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
// Validate email
if (filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo $email." is a valid email address";
} else {
echo $email." is not a valid email address";
}
?>
Use:
or "filter_var" from http://php.net/manual/en/function.filter-var.php
var_dump(filter_var('bob#example.com', FILTER_VALIDATE_EMAIL));
or "EmailValidator" from https://github.com/egulias/EmailValidator
$validator = new EmailValidator();
$multipleValidations = new MultipleValidationWithAnd([
new RFCValidation(),
new DNSCheckValidation()
]);
$validator->isValid("example#example.com", $multipleValidations); //true
take several care, a address as iasd#x.z-----com is INVALID, but filter_var() return true, many others strings (emails) INVALIDS return true using filter_var().
for validate email I use this function:
function correcorre($s){// correo correcto
$x = '^([[:alnum:]](_|-|\.)*)*[[:alnum:]]+#([[:alnum:]]+(-|\.)+)*[[:alnum:]]+\.[[:alnum:]]+$';
preg_match("!$x!i", $s, $M);
if(!empty($M[0]))return($M[0]);
}
please improve and share, thanks