| 1303 | | function is_email($user_email) { |
| 1304 | | $chars = "/^([a-z0-9+_]|\\-|\\.)+@(([a-z0-9_]|\\-)+\\.)+[a-z]{2,6}\$/i"; |
| 1305 | | if (strpos($user_email, '@') !== false && strpos($user_email, '.') !== false) { |
| 1306 | | if (preg_match($chars, $user_email)) { |
| 1307 | | return true; |
| 1308 | | } else { |
| 1309 | | return false; |
| | 1306 | function is_email( $email, $check_dns = false ) |
| | 1307 | { |
| | 1308 | // Test for the minimum length the email can be |
| | 1309 | if ( strlen( $email ) < 3 ) { |
| | 1310 | return apply_filters( 'is_email', false, $email, 'email_too_short' ); |
| | 1311 | } |
| | 1312 | |
| | 1313 | // Test for an @ character after the first position |
| | 1314 | if ( strpos( $email, '@', 1 ) === false ) { |
| | 1315 | return apply_filters( 'is_email', false, $email, 'email_no_at' ); |
| | 1316 | } |
| | 1317 | |
| | 1318 | // Split out the local and domain parts |
| | 1319 | list( $local, $domain ) = explode( '@', $email, 2 ); |
| | 1320 | |
| | 1321 | // LOCAL PART |
| | 1322 | // Test for invalid characters |
| | 1323 | if ( !preg_match( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', $local ) ) { |
| | 1324 | return apply_filters( 'is_email', false, $email, 'local_invalid_chars' ); |
| | 1325 | } |
| | 1326 | |
| | 1327 | // DOMAIN PART |
| | 1328 | // Test for sequences of periods |
| | 1329 | if ( preg_match( '/\.{2,}/', $domain ) ) { |
| | 1330 | return apply_filters( 'is_email', false, $email, 'domain_period_sequence' ); |
| | 1331 | } |
| | 1332 | |
| | 1333 | // Test for leading and trailing periods and whitespace |
| | 1334 | if ( trim( $domain, " \t\n\r\0\x0B." ) !== $domain ) { |
| | 1335 | return apply_filters( 'is_email', false, $email, 'domain_period_limits' ); |
| | 1336 | } |
| | 1337 | |
| | 1338 | // Split the domain into subs |
| | 1339 | $subs = explode( '.', $domain ); |
| | 1340 | |
| | 1341 | // Assume the domain will have at least two subs |
| | 1342 | if ( 2 > count( $subs ) ) { |
| | 1343 | return apply_filters( 'is_email', false, $email, 'domain_no_periods' ); |
| | 1344 | } |
| | 1345 | |
| | 1346 | // Loop through each sub |
| | 1347 | foreach ( $subs as $sub ) { |
| | 1348 | // Test for leading and trailing hyphens and whitespace |
| | 1349 | if ( trim( $sub, " \t\n\r\0\x0B-" ) !== $sub ) { |
| | 1350 | return apply_filters( 'is_email', false, $email, 'sub_hyphen_limits' ); |
| 1451 | | function sanitize_email($email) { |
| 1452 | | return preg_replace('/[^a-z0-9+_.@-]/i', '', $email); |
| | 1504 | function sanitize_email( $email ) |
| | 1505 | { |
| | 1506 | // Test for the minimum length the email can be |
| | 1507 | if ( strlen( $email ) < 3 ) { |
| | 1508 | return apply_filters( 'sanitize_email', '', $email, 'email_too_short' ); |
| | 1509 | } |
| | 1510 | |
| | 1511 | // Test for an @ character after the first position |
| | 1512 | if ( strpos( $email, '@', 1 ) === false ) { |
| | 1513 | return apply_filters( 'sanitize_email', '', $email, 'email_no_at' ); |
| | 1514 | } |
| | 1515 | |
| | 1516 | // Split out the local and domain parts |
| | 1517 | list( $local, $domain ) = explode( '@', $email, 2 ); |
| | 1518 | |
| | 1519 | // LOCAL PART |
| | 1520 | // Test for invalid characters |
| | 1521 | $local = preg_replace( '/^[a-zA-Z0-9!#$%&\'*+\/=?^_`{|}~\.-]+$/', '', $local ); |
| | 1522 | if ( '' === $local ) { |
| | 1523 | return apply_filters( 'sanitize_email', '', $email, 'local_invalid_chars' ); |
| | 1524 | } |
| | 1525 | |
| | 1526 | // DOMAIN PART |
| | 1527 | // Test for sequences of periods |
| | 1528 | $domain = preg_replace( '/\.{2,}/', '', $domain ); |
| | 1529 | if ( '' === $domain ) { |
| | 1530 | return apply_filters( 'sanitize_email', '', $email, 'domain_period_sequence' ); |
| | 1531 | } |
| | 1532 | |
| | 1533 | // Test for leading and trailing periods and whitespace |
| | 1534 | $domain = trim( $domain, " \t\n\r\0\x0B." ); |
| | 1535 | if ( '' === $domain ) { |
| | 1536 | return apply_filters( 'sanitize_email', '', $email, 'domain_period_limits' ); |
| | 1537 | } |
| | 1538 | |
| | 1539 | // Split the domain into subs |
| | 1540 | $subs = explode( '.', $domain ); |
| | 1541 | |
| | 1542 | // Assume the domain will have at least two subs |
| | 1543 | if ( 2 > count( $subs ) ) { |
| | 1544 | return apply_filters( 'sanitize_email', '', $email, 'domain_no_periods' ); |
| | 1545 | } |
| | 1546 | |
| | 1547 | // Create an array that will contain valid subs |
| | 1548 | $new_subs = array(); |
| | 1549 | |
| | 1550 | // Loop through each sub |
| | 1551 | foreach ( $subs as $sub ) { |
| | 1552 | // Test for leading and trailing hyphens |
| | 1553 | $sub = trim( $sub, " \t\n\r\0\x0B-" ); |
| | 1554 | |
| | 1555 | // Test for invalid characters |
| | 1556 | $sub = preg_replace( '/^[^a-z0-9-]+$/i', '', $sub ); |
| | 1557 | |
| | 1558 | // If there's anything left, add it to the valid subs |
| | 1559 | if ( '' !== $sub ) { |
| | 1560 | $new_subs[] = $sub; |
| | 1561 | } |
| | 1562 | } |
| | 1563 | |
| | 1564 | // If there aren't 2 or more valid subs |
| | 1565 | if ( 2 > count( $new_subs ) ) { |
| | 1566 | return apply_filters( 'sanitize_email', '', $email, 'domain_no_valid_subs' ); |
| | 1567 | } |
| | 1568 | |
| | 1569 | // Join valid subs into the new domain |
| | 1570 | $domain = join( '.', $new_subs ); |
| | 1571 | |
| | 1572 | // Put the email back together |
| | 1573 | $email = $local . '@' . $domain; |
| | 1574 | |
| | 1575 | // Congratulations your email made it! |
| | 1576 | return apply_filters( 'sanitize_email', $email, $email, null ); |