Detailed usage of php split of function

  • 2020-06-07 04:06:55
  • OfStack

The basic syntax of PHP function split() is: array split (string $pattern, string $string [, int $limit]). We gave you two examples of how to use this function.

For beginners, mastering the use of common functions in PHP is the foundation of their continued learning. Today we are going to give you some details about the use of PHP function split(). I hope you can increase your knowledge base through this article.
instructions
array split ( string $pattern, string $string [, int $limit] )
prompt
The preg_split() function USES Perl compliant regular expression syntax and is usually a faster alternative to the PHP function split(). If you don't need the power of regular expressions, it's faster to use explode() so you don't incur the waste of the regular expression engine.
This function returns an array of strings, each of which is a substring delimited by string, a case-sensitive regular expression. If limit is set, the returned array contains a maximum of limit cells, with the last cell containing all the rest of string. If an error occurs, split() returns FALSE.
Split the first four fields in /etc/passwd:
Example 1839. PHP ()
Dive into the details of PHP nl2br() formatted output

<?php list($user, $pass, $uid, $gid, $extra) =  split (":", $passwd_line, 5);  ?> 

If n matches pattern in the string, the returned array will contain n+1 cell. For example, if pattern is not found, a 1-cell array is returned. Of course, this is also true if string is empty.
Parse dates that may be split with slashes, dots, or horizontal lines:
Examples of the 1840. PHP function split()

<?php 
   //  The delimiter can be a slash, dot, or horizontal line  
   $date = "04/30/1973";  
   list($month, $day, $year) = split ('[/.-]', $date); 
   echo "Month: $month; Day: $day; Year: $year<br />\n";
?> 

To emulate the @chars = split(", $str) behavior in Perl, see the example in the preg_split() or str_split() function.
Note that pattern is a regular expression. If the split character you want to use is a special character in a regular expression, escape it first. If you find the PHP function split()(or any other regex function) odd, read the regex.7 file contained in the PHP distribution under the regex/ subdirectory. This file is the manual page format, you can use similar man/usr/local/src regex/regex. 7 commands to read

Related articles: