Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Perl-Compatible Regular Expressions (PCRE)
 
Article Index
Perl-Compatible Regular Expressions (PCRE)
Page 2
Page 3
Page 4
Page 5
Page 6
Page 7
Page 8
Page 9
Page 10
.

Table 3.2. preg_split() Flags

Reference Number

Value

PREG_SPLIT_NO_EMPTY

Causes empty substrings to be discarded.

PREG_SPLIT_DELIM_CAPTURE

Causes any references inside pattern to be captured and returned as part of the function's output.

PREG_SPLIT_OFFSET_CAPTURE

Causes the position of each substring to be returned as part of the function's output (similar to PREG_OFFSET_CAPTURE in preg_match()).


Here's an example of how preg_split() can be used:

<?php

$s = 'Ten times he called, and ten times nobody answered';

var_dump (preg_split ('/[ ,]/', $s));

?>

This script causes the string $s to be split whenever either a space or a comma is found, resulting in the following output:

array(10) {
[0]=>
string(3) "Ten"
[1]=>
string(5) "times"
[2]=>
string(2) "he"
[3]=>
string(6) "called"
[4]=>
string(0) ""
[5]=>
string(3) "and"
[6]=>
string(3) "ten"
[7]=>
string(5) "times"
[8]=>
string(6) "nobody"
[9]=>
string(8) "answered"
}

As you can imagine, the explode() function by itself would have been inadequate in this case, because it would have been able to split $s based only on a single character.



Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Cool Graphic Wallpaper 35
Statistic


Last Post

 
Top! Top!