ホームページ >php教程 >php手册 >PHP explode() 函数源码分析

PHP explode() 函数源码分析

WBOY
WBOYオリジナル
2016-06-06 19:55:331413ブラウズ

在PHP网站开发中,我们经常需要把一个字符串分割成数组,这时候我们经常会使用 PHP explode() 函数来帮我们实现。这篇文章我们主要来学学 explode() 函数是怎么工作的。 文件1:ext/standard/string.c (explode的源代码) PHP_FUNCTION ( explode ) { char

在PHP网站开发中,我们经常需要把一个字符串分割成数组,这时候我们经常会使用 PHP explode() 函数来帮我们实现。这篇文章我们主要来学学 explode() 函数是怎么工作的。

 

文件1:ext/standard/string.c (explode的源代码)

 

<span>PHP_FUNCTION</span><span>(</span><span>explode</span><span>)</span><span>{</span><span>char</span><span>*</span><span>str</span><span>,</span><span>*</span><span>delim</span><span>;</span><span>int</span><span> str_len </span><span>=</span><span>0</span><span>,</span><span> delim_len </span><span>=</span><span>0</span><span>;</span><span>long</span><span> limit </span><span>=</span><span> LONG_MAX</span><span>;</span><span>/* No limit */</span><span>
    zval zdelim</span><span>,</span><span> zstr</span><span>;</span><span>if</span><span>(</span><span>zend_parse_parameters</span><span>(</span><span>ZEND_NUM_ARGS</span><span>()</span><span> TSRMLS_CC</span><span>,</span><span>"ss|l"</span><span>,</span><span>&</span><span>delim</span><span>,</span><span>&</span><span>delim_len</span><span>,</span><span>&</span><span>str</span><span>,</span><span>&</span><span>str_len</span><span>,</span><span>&</span><span>limit</span><span>)</span><span>==</span><span> FAILURE</span><span>)</span><span>{</span><span>return</span><span>;</span><span>}</span><span>if</span><span>(</span><span>delim_len </span><span>==</span><span>0</span><span>)</span><span>{</span><span>
        php_error_docref</span><span>(</span><span>NULL TSRMLS_CC</span><span>,</span><span> E_WARNING</span><span>,</span><span>"Empty delimiter"</span><span>);</span><span>
        RETURN_FALSE</span><span>;</span><span>}</span><span>// 这里会开辟一个数组,用来存放分割后的数据</span><span>
    array_init</span><span>(</span><span>return_value</span><span>);</span><span>// 因为这个,我们用explode('|', '');成为了合法的</span><span>if</span><span>(</span><span>str_len </span><span>==</span><span>0</span><span>)</span><span>{</span><span>if</span><span>(</span><span>limit </span><span>>=</span><span>0</span><span>)</span><span>{</span><span>
            add_next_index_stringl</span><span>(</span><span>return_value</span><span>,</span><span>""</span><span>,</span><span>sizeof</span><span>(</span><span>""</span><span>)</span><span>-</span><span>1</span><span>,</span><span>1</span><span>);</span><span>}</span><span>return</span><span>;</span><span>}</span><span>// 下面这两个是将原字串和分割符都构建成_zval_struct 结构,</span><span>// ZVAL_STRINGL会分配空间~~源代码随后贴出</span><span>
    ZVAL_STRINGL</span><span>(&</span><span>zstr</span><span>,</span><span> str</span><span>,</span><span> str_len</span><span>,</span><span>0</span><span>);</span><span>   
    ZVAL_STRINGL</span><span>(&</span><span>zdelim</span><span>,</span><span> delim</span><span>,</span><span> delim_len</span><span>,</span><span>0</span><span>);</span><span>// limit值是explode中允许传递的explode的第三个参数,它允许正负</span><span>if</span><span>(</span><span>limit </span><span>></span><span>1</span><span>)</span><span>{</span><span>
        php_explode</span><span>(&</span><span>zdelim</span><span>,</span><span>&</span><span>zstr</span><span>,</span><span> return_value</span><span>,</span><span> limit</span><span>);</span><span>}</span><span>else</span><span>if</span><span>(</span><span>limit </span><span><span>0</span><span>)</span><span>{</span><span>
        php_explode_negative_limit</span><span>(&</span><span>zdelim</span><span>,</span><span>&</span><span>zstr</span><span>,</span><span> return_value</span><span>,</span><span> limit</span><span>);</span><span>}</span><span>else</span><span>{</span><span>
        add_index_stringl</span><span>(</span><span>return_value</span><span>,</span><span>0</span><span>,</span><span> str</span><span>,</span><span> str_len</span><span>,</span><span>1</span><span>);</span><span>}</span><span>}</span></span>
 
文章摘自 PHP explode() 函数源码分析
声明:
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。