Function: preg_split
(PHP 3 >= 3.0.9, PHP 4, PHP 5)
preg_split -- Split string by a regular expression
Description
array
preg_split
( string pattern, string subject [, int limit [, int flags]] )
Returns an array containing substrings of
subject
split along boundaries matched by
pattern
.
If
limit
is specified, then only substrings up to
limit
are returned, and if
limit
is -1, it actually means "no limit", which is
useful for specifying the
flags
.
flags
can be any combination of the following flags
(combined with bitwise | operator):
PREG_SPLIT_NO_EMPTY
If this flag is set, only non-empty pieces will be returned by
preg_split()
.
PREG_SPLIT_DELIM_CAPTURE
If this flag is set, parenthesized expression in the delimiter pattern
will be captured and returned as well. This flag was added for 4.0.5.
PREG_SPLIT_OFFSET_CAPTURE
If this flag is set, for every occurring match the appendant string
offset will also be returned. Note that this changes the return
value in an array where every element is an array consisting of the
matched string at offset
0
and its string offset
into
subject
at offset
1
.
This flag is available since PHP 4.3.0 .
Example 1.
preg_split()
example : Get the parts of a search string
<?php
// split the phrase by any number of commas or space characters,
// which include " ", \r, \t, \n and \f
$keywords = preg_split("/[\s,]+/", "hypertext language, programming");
?>
Example 2. Splitting a string into component characters
<?php
$str = 'string';
$chars = preg_split('//', $str, -1, PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>
Example 3. Splitting a string into matches and their offsets
<?php
$str = 'hypertext language programming';
$chars = preg_split('/ /', $str, -1, PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>
will yield:
Array
(
[0] => Array
(
[0] => hypertext
[1] => 0
)
[1] => Array
(
[0] => language
[1] => 10
)
[2] => Array
(
[0] => programming
[1] => 19
)
)
Note:
Parameter
flags
was added in PHP 4 Beta 3.
Related Function(s)
spliti() split() implode() preg_match() preg_match_all() preg_replace() pdf_get_font() pdf_get_value() pdf_get_fontsize() pdf_get_fontname() pdf_get_parameter() pdf_set_info_creator() pdf_set_info() pdf_set_info_title() pdf_set_info_subject() pdf_set_info_author() pdf_set_info_keywords() pdf_set_leading() pdf_set_value() pdf_set_text_rendering() pdf_set_text_rise() pdf_set_horiz_scaling() pdf_set_text_matrix() pdf_set_char_spacing() pdf_set_word_spacing() pdf_set_parameter() pdf_open() pdf_new() pdf_open_file() pdf_set_font() pdf_findfont() pdf_setfont() pdf_set_duration() pdf_open_gif() pdf_open_image_file() pdf_open_jpeg() pdf_open_tiff() pdf_open_png() pdf_get_image_width() pdf_get_image_height() pdf_add_annotation() pdf_add_bookmark() pdf_add_launchlink() pdf_add_locallink() pdf_add_note() pdf_add_outline() pdf_add_pdflink() pdf_add_thumbnail() pdf_add_weblink() pdf_arc() pdf_arcn() pdf_attach_file() pdf_begin_page() pdf_begin_pattern() pdf_begin_template() pdf_circle() pdf_clip() pdf_close_image() pdf_close_pdi_page() pdf_close_pdi() pdf_close() pdf_closepath_fill_stroke() pdf_closepath_stroke() pdf_closepath() pdf_concat() pdf_continue_text() pdf_curveto() pdf_delete() pdf_end_page() pdf_end_pattern() pdf_end_template() pdf_endpath() pdf_fill_stroke() pdf_fill() pdf_get_buffer() pdf_get_majorversion() pdf_get_minorversion() pdf_get_pdi_parameter() pdf_get_pdi_value() pdf_initgraphics() pdf_lineto() pdf_makespotcolor() pdf_moveto() pdf_open_ccitt() pdf_open_image() pdf_open_memory_image() pdf_open_pdi_page() pdf_open_pdi() pdf_place_image() pdf_place_pdi_page() pdf_rect() pdf_restore() pdf_rotate() pdf_save() pdf_scale() pdf_set_border_color() pdf_set_border_dash() pdf_set_border_style() pdf_set_text_pos() pdf_setcolor() pdf_setdash() pdf_setflat() pdf_setgray_fill() pdf_setgray_stroke() pdf_setgray() pdf_setlinecap() pdf_setlinejoin() pdf_setlinewidth() pdf_setmatrix() pdf_setmiterlimit() pdf_setpolydash() pdf_setrgbcolor_fill() pdf_setrgbcolor_stroke() pdf_setrgbcolor() pdf_show_boxed() pdf_show_xy() pdf_show() pdf_skew() pdf_stringwidth() pdf_stroke() pdf_translate()