首頁 > php教程 > php手册 > PHP explode() 函数源码分析

PHP explode() 函数源码分析

WBOY
發布: 2016-06-06 19:55:33
原創
1412 人瀏覽過

在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() 函数源码分析
登入後複製
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門推薦
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板