Home > Backend Development > PHP Tutorial > Correct use of PHP function preg_split_PHP tutorial

Correct use of PHP function preg_split_PHP tutorial

WBOY
Release: 2016-07-15 13:30:19
Original
1141 people have browsed it

For beginners, everyone explains about array preg_split ( string $pattern, string $subject [, int $limit [, int $flags]] )

Returns an array containing the substrings of subject split along boundaries that match pattern.

If limit is specified, at most limit substrings will be returned. If limit is -1, it means there is no limit and can be used to continue specifying optional parameter flags.

flags can be any combination of the following flags (combined with the bitwise OR operator |):

PREG_SPLIT_NO_EMPTY
If this flag is set, preg_split() will only return non-empty ingredients.
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, bracket expressions in the delimiter pattern will also be captured and returned. This tag was added in PHP 4.0.5.
PREG_SPLIT_OFFSET_CAPTURE
If this flag is set, the associated string offset of each matching result will also be returned. Note that this changes the value of the returned array so that each cell in it is also an array, where the first item is the matched string and the second item is its offset within the subject. This tag is available since PHP 4.3.0.

Tip

If you don’t need the power of regular expressions, you can choose to use faster (and simpler) alternatives such as explode() or str_split().

Example 1672. PHP function preg_split example: Get the components of the search string

<ol class="dp-xml">
<li class="alt"><span><span>// split the phrase by any number of commas or space characters,   </span></span></li>
<li><span>// which include " ", r, t, n and f   </span></li>
<li class="alt">
<span>$</span><span class="attribute">keywords</span><span> = </span><span class="attribute-value">preg_split</span><span> ("/[s,]+/", "hypertext language, programming");   </span>
</li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login

Example 1673. PHP function preg_split Will string Split into characters

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'string'</span><span>;   </span></span></li>
<li>
<span>$</span><span class="attribute">chars</span><span> = </span><span class="attribute-value">preg_split</span><span>('//', $str, -1, PREG_SPLIT_NO_EMPTY);   </span>
</li>
<li class="alt"><span>print_r($chars);   </span></li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login
Copy after login

Example 1674. PHP function preg_split splits the string into matches and their offsets

<ol class="dp-xml">
<li class="alt"><span><span>$</span><span class="attribute">str</span><span> = </span><span class="attribute-value">'string'</span><span>;   </span></span></li>
<li>
<span>$</span><span class="attribute">chars</span><span> = </span><span class="attribute-value">preg_split</span><span>('//', $str, -1, PREG_SPLIT_NO_EMPTY);   </span>
</li>
<li class="alt"><span>print_r($chars);   </span></li>
<li>
<span class="tag">?></span><span>  </span>
</li>
</ol>
Copy after login
Copy after login

The PHP function preg_split example will output:

<ol class="dp-xml">
<li class="alt"><span><span>Array( [0] =</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span>Array ( [0] =</span><span class="tag">></span><span> hypertext [1] =</span><span class="tag">></span><span> 0 ) [1] =</span><span class="tag">></span><span> </span></span></li>
<li class="alt"><span><span>Array ( [0] =</span><span class="tag">></span><span> language [1] =</span><span class="tag">></span><span> 10 ) [2] =</span><span class="tag">></span><span> </span></span></li>
<li class="alt">
<span><span>Array ( [0] =</span><span class="tag">></span><span> programming [1] =</span><span class="tag">></span><span> 19 </span></span><span><span>)</span></span><span><span>) </span></span>
</li>
</ol>
Copy after login


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/446302.htmlTechArticleFor beginners, everyone explains array preg_split ( string $pattern, string $subject [, int $limit [, int $flags]] ) returns an array containing the subject along with pattern...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template