PHP';内部实现的内置功能?

PHP';内部实现的内置功能?,php,function,php-internals,filter-var,Php,Function,Php Internals,Filter Var,这些函数的编写方式是否与用户函数相同?我是说用PHP代码和正则表达式之类的东西 例如: filter\u var($email,filter\u VALIDATE\u email) vs PHP是用C编写的。PHP函数是用高质量的C代码编写的,然后经过编译形成PHP语言库 如果要扩展PHP(编辑/写入)自己的函数,请检查以下内容: 编辑: 给你: 这是函数的原始C代码: /* {{{ proto mixed filter_var(mixed variable [, long filter [,

这些函数的编写方式是否与用户函数相同?我是说用PHP代码和正则表达式之类的东西

例如:

filter\u var($email,filter\u VALIDATE\u email)

vs


PHP是用C编写的。PHP函数是用高质量的C代码编写的,然后经过编译形成PHP语言库

如果要扩展PHP(编辑/写入)自己的函数,请检查以下内容:

编辑:

给你:

这是函数的原始C代码:

/* {{{ proto mixed filter_var(mixed variable [, long filter [, mixed options]])
 * Returns the filtered version of the vriable.
 */
PHP_FUNCTION(filter_var)
{
    long filter = FILTER_DEFAULT;
    zval **filter_args = NULL, *data;

    if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z/|lZ", &data, &filter, &filter_args) == FAILURE) {
        return;
    }

    if (!PHP_FILTER_ID_EXISTS(filter)) {
        RETURN_FALSE;
    }

    MAKE_COPY_ZVAL(&data, return_value);

    php_filter_call(&return_value, filter, filter_args, 1, FILTER_REQUIRE_SCALAR TSRMLS_CC);
}
/* }}} */



static void php_filter_call(zval **filtered, long filter, zval **filter_args, const int copy, long filter_flags TSRMLS_DC) /* {{{ */
{
    zval  *options = NULL;
    zval **option;
    char  *charset = NULL;

    if (filter_args && Z_TYPE_PP(filter_args) != IS_ARRAY) {
        long lval;

        PHP_FILTER_GET_LONG_OPT(filter_args, lval);

        if (filter != -1) { /* handler for array apply */
            /* filter_args is the filter_flags */
            filter_flags = lval;

            if (!(filter_flags & FILTER_REQUIRE_ARRAY ||  filter_flags & FILTER_FORCE_ARRAY)) {
                filter_flags |= FILTER_REQUIRE_SCALAR;
            }
        } else {
            filter = lval;
        }
    } else if (filter_args) {
        if (zend_hash_find(HASH_OF(*filter_args), "filter", sizeof("filter"), (void **)&option) == SUCCESS) {
            PHP_FILTER_GET_LONG_OPT(option, filter);
        }

        if (zend_hash_find(HASH_OF(*filter_args), "flags", sizeof("flags"), (void **)&option) == SUCCESS) {
            PHP_FILTER_GET_LONG_OPT(option, filter_flags);

            if (!(filter_flags & FILTER_REQUIRE_ARRAY ||  filter_flags & FILTER_FORCE_ARRAY)) {
                filter_flags |= FILTER_REQUIRE_SCALAR;
            }
        }

        if (zend_hash_find(HASH_OF(*filter_args), "options", sizeof("options"), (void **)&option) == SUCCESS) {
            if (filter != FILTER_CALLBACK) {
                if (Z_TYPE_PP(option) == IS_ARRAY) {
                    options = *option;
                }
            } else {
                options = *option;
                filter_flags = 0;
            }
        }
    }

    if (Z_TYPE_PP(filtered) == IS_ARRAY) {
        if (filter_flags & FILTER_REQUIRE_SCALAR) {
            if (copy) {
                SEPARATE_ZVAL(filtered);
            }
            zval_dtor(*filtered);
            if (filter_flags & FILTER_NULL_ON_FAILURE) {
                ZVAL_NULL(*filtered);
            } else {
                ZVAL_FALSE(*filtered);
            }
            return;
        }
        php_zval_filter_recursive(filtered, filter, filter_flags, options, charset, copy TSRMLS_CC);
        return;
    }
    if (filter_flags & FILTER_REQUIRE_ARRAY) {
        if (copy) {
            SEPARATE_ZVAL(filtered);
        }
        zval_dtor(*filtered);
        if (filter_flags & FILTER_NULL_ON_FAILURE) {
            ZVAL_NULL(*filtered);
        } else {
            ZVAL_FALSE(*filtered);
        }
        return;
    }

    php_zval_filter(filtered, filter, filter_flags, options, charset, copy TSRMLS_CC);
    if (filter_flags & FILTER_FORCE_ARRAY) {
        zval *tmp;

        ALLOC_ZVAL(tmp);
        MAKE_COPY_ZVAL(filtered, tmp);

        zval_dtor(*filtered);

        array_init(*filtered);
        add_next_index_zval(*filtered, tmp);
    }
}
下面是验证电子邮件的例行程序: --这回答了你的问题,由regex内部完成

void php_filter_validate_email(PHP_INPUT_FILTER_PARAM_DECL) /* {{{ */
{
    /*
     * The regex below is based on a regex by Michael Rushton.
     * However, it is not identical.  I changed it to only consider routeable
     * addresses as valid.  Michael's regex considers a@b a valid address
     * which conflicts with section 2.3.5 of RFC 5321 which states that:
     *
     *   Only resolvable, fully-qualified domain names (FQDNs) are permitted
     *   when domain names are used in SMTP.  In other words, names that can
     *   be resolved to MX RRs or address (i.e., A or AAAA) RRs (as discussed
     *   in Section 5) are permitted, as are CNAME RRs whose targets can be
     *   resolved, in turn, to MX or address RRs.  Local nicknames or
     *   unqualified names MUST NOT be used.
     *
     * This regex does not handle comments and folding whitespace.  While
     * this is technically valid in an email address, these parts aren't
     * actually part of the address itself.
     *
     * Michael's regex carries this copyright:
     *
     * Copyright © Michael Rushton 2009-10
     * http://squiloople.com/
     * Feel free to use and redistribute this code. But please keep this copyright notice.
     *
     */
    const char regexp[] = "/^(?!(?:(?:\\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";

    pcre       *re = NULL;
    pcre_extra *pcre_extra = NULL;
    int preg_options = 0;
    int         ovector[150]; /* Needs to be a multiple of 3 */
    int         matches;


    /* The maximum length of an e-mail address is 320 octets, per RFC 2821. */
    if (Z_STRLEN_P(value) > 320) {
        RETURN_VALIDATION_FAILED
    }

    re = pcre_get_compiled_regex((char *)regexp, &pcre_extra, &preg_options TSRMLS_CC);
    if (!re) {
        RETURN_VALIDATION_FAILED
    }
    matches = pcre_exec(re, NULL, Z_STRVAL_P(value), Z_STRLEN_P(value), 0, 0, ovector, 3);

    /* 0 means that the vector is too small to hold all the captured substring offsets */
    if (matches < 0) {
        RETURN_VALIDATION_FAILED
    }

}
/* }}} */
void php\u filter\u validate\u email(php\u INPUT\u filter\u PARAM\u DECL)/*{{*/
{
/*
*下面的正则表达式基于Michael Rushton的正则表达式。
但是,它不是相同的。我把它改为只考虑可路由。
*地址有效。Michael的正则表达式考虑a@b有效地址
*与RFC 5321第2.3.5节冲突,该节规定:
*
*仅允许解析的完全限定域名(FQDN)
*当SMTP中使用域名时。换句话说,可以
*解析为MX RRs或地址(即A或AAAA)RRs(如上所述
*在第5)节中,以及目标可以
*依次解析为MX或地址RRs。本地昵称或
*不得使用非限定名称。
*
*此正则表达式不处理注释和折叠空格
*这在技术上是有效的电子邮件地址,这些部分不是
*实际上是地址本身的一部分。
*
*Michael的regex拥有以下版权:
*
*版权所有©迈克尔·拉什顿2009-10
* http://squiloople.com/
*请随意使用和重新发布此代码。但请保留此版权声明。
*
*/
(2)目前,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,(?:\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]。(?:\\x5C[\\x00-\\x7F]))*\\(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(3)(3)(3)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(2)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(3)(9]+)*\\){1126}{1,}(?:(?:[a-z][a-z0-9]*)。(?:(?:(?:xn-)[a-z0-9]+)(?:-[a-z0-9]+)*)|(?:\[(?::):以下::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::(((((((::::::::[a-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-0-10-0-10-0-10-10-10-10-9[[[[[[10-0-0-0-0-10-10-10-10-10-9[[[[[[[[[[10-0-0-0-9[(?:(?:(?:[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 1,4}:25})[0-5][0-9])(?: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“;
pcre*re=NULL;
pcre_extra*pcre_extra=NULL;
int preg_选项=0;
int-ovector[150];/*需要是3的倍数*/
整数匹配;
/*根据RFC 2821,电子邮件地址的最大长度为320个八位字节*/
如果(Z_STRLEN_P(值)>320){
返回\验证\失败
}
re=pcre_get_compiled_regex((char*)regexp,&pcre_extra,&preg_options TSRMLS_CC);
如果(!re){
返回\验证\失败
}
matches=pcre_exec(re,NULL,Z_STRVAL_P(值),Z_STRLEN_P(值),0,0,ovector,3);
/*0表示向量太小,无法容纳所有捕获的子字符串偏移*/
如果(匹配<0){
返回\验证\失败
}
}
/* }}} */

PHP函数可以是:

  • 用C编写,而不是用PHP编写
  • 或者只是对其他库提供的函数进行包装(例如,PHP的curl扩展只是curl库周围的包装)

如果您感到好奇,可以看看PHP的源代码——这里是它的SVN:


例如,
filter\u var()
函数应该在源代码中的某个地方定义。

Nope.PHP内部函数是用C编写的,而不是用PHP代码编写的。由于Zend运行时宏太多,参数是如何从PHP传输到C结构的,这看起来相当笨拙

该特定函数确实使用正则表达式。它也是一个很好的示例:


在中间的某个地方查找<代码> ReXEP []/COD>。E/P> ReGEX字符串是巨大的。PHP版本不是更快吗?