downloads | documentation | faq | getting help | mailing lists | licenses | wiki | reporting bugs | php.net sites | links | conferences | my php.net

search for in the

POSIX Regex> <preg_replace
Last updated: Fri, 24 Jul 2009

view this page in

preg_split

(PHP 4, PHP 5)

preg_split정규 표현식에 따라 문자열을 나눔

설명

array preg_split ( string $pattern , string $subject [, int $limit [, int $flags ]] )

정규 표현식에 따라서 주어진 문자열을 나눕니다.

인수

pattern

검색할 패턴 문자열.

subject

입력 문자열.

limit

지정하면, limit 회까지 나눠진 문자열을 반환하며, limit 가 -1이면 "무제한"을 의미합니다. 이 값은 flags 를 지정할 때 유용합니다.

flags

flags 는 다음 플래그들을 조합할 수 있습니다 (bitwise | 연산자로 조합합니다) :

PREG_SPLIT_NO_EMPTY
이 플래그를 설정하면, preg_split()에 의해 나눈 후 비어있지 않은 조각만을 반환합니다.
PREG_SPLIT_DELIM_CAPTURE
이 플래그를 설정하면, 구분자 패턴 안의 서브패턴도 검출하여 반환합니다.
PREG_SPLIT_OFFSET_CAPTURE

이 플래그를 설정하면, 문자열의 시작 위치도 반환합니다. 반환값이 매치된 문자열을 오프셋 0으로, 문자열 시작 위치를 오프셋 1로 가지는 배열을 원소로 갖는 배열로 변하는 점에 주의하십시오.

반환값

pattern 에 매치한 경계로 나눠진 subject 의 부분을 가진 배열을 반환합니다.

변경점

버전 설명
4.3.0 PREG_SPLIT_OFFSET_CAPTURE 추가
4.0.5 PREG_SPLIT_DELIM_CAPTURE 추가
4.0.0 flags 인수 추가

예제

Example #1 preg_split() 예제 : 검색 문자열의 일부만을 얻기

<?php
// " ", \r, \t, \n, \f를 포함하여
// 임의 갯수의 콤마와 스페이스로 구문을 나눕니다.
$keywords preg_split("/[\s,]+/""hypertext language, programming");
?>

Example #2 문자열을 구성 문자로 나누기.

<?php
$str 
'string';
$chars preg_split('//'$str, -1PREG_SPLIT_NO_EMPTY);
print_r($chars);
?>

Example #3 매치와 시작위치로 문자열을 나누기.

<?php
$str 
'hypertext language programming';
$chars preg_split('/ /'$str, -1PREG_SPLIT_OFFSET_CAPTURE);
print_r($chars);
?>

위 예제의 출력:

Array
(
    [0] => Array
        (
            [0] => hypertext
            [1] => 0
        )

    [1] => Array
        (
            [0] => language
            [1] => 10
        )

    [2] => Array
        (
            [0] => programming
            [1] => 19
        )

)

주의

Tip

정규 표현식의 힘이 필요하지 않으면, 더 빠른(그리고 간단한) explode()str_split()로 대체할 수 있습니다.

참고

  • spliti() - Split string into array by regular expression case insensitive
  • split() - Split string into array by regular expression
  • implode() - 문자열로 배열 원소를 결합
  • preg_match() - 정규표현식 매치를 수행
  • preg_match_all() - 전역 정규 표현식 매치를 수행합니다
  • preg_replace() - 정규 표현식 검색과 치환을 수행



POSIX Regex> <preg_replace
Last updated: Fri, 24 Jul 2009
 
add a note add a note User Contributed Notes
preg_split
jan dot sochor at icebolt dot info
24-Oct-2009 10:26
Sometimes PREG_SPLIT_DELIM_CAPTURE does strange results.

<?php
$content
= '<strong>Lorem ipsum dolor</strong> sit <img src="test.png" />amet <span class="test" style="color:red">consec<i>tet</i>uer</span>.';
$chars = preg_split('/<[^>]*[^\/]>/i', $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($chars);
?>
Produces:
Array
(
    [0] => Lorem ipsum dolor
    [1] =>  sit <img src="test.png" />amet
    [2] => consec
    [3] => tet
    [4] => uer
)

So that the delimiter patterns are missing. If you wanna get these patters remember to use parentheses.

<?php
$chars
= preg_split('/(<[^>]*[^\/]>)/i', $content, -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($chars); //parentheses added
?>
Produces:
Array
(
    [0] => <strong>
    [1] => Lorem ipsum dolor
    [2] => </strong>
    [3] =>  sit <img src="test.png" />amet
    [4] => <span class="test" style="color:red">
    [5] => consec
    [6] => <i>
    [7] => tet
    [8] => </i>
    [9] => uer
    [10] => </span>
    [11] => .
)
php at dmi dot me dot uk
06-Oct-2009 08:23
To split a camel-cased string using preg_split() with lookaheads and lookbehinds:

<?php
function splitCamelCase($str) {
  return
preg_split('/(?<=\\w)(?=[A-Z])/', $str);
}
?>
Peter -the pete- de Pijd
24-Sep-2009 09:34
If you want to use something like explode(PHP_EOL, $string) but for all combinations of \r and \n, try this one:

<?php
$text
= "A\nB\rC\r\nD\r\rE\n\nF";
$texts = preg_split("/((\r(?!\n))|((?<!\r)\n)|(\r\n))/", $text);
?>

result:
array("A", "B", "C", "D", "", "E", "", "F");
buzoganylaszlo at yahoo dot com
01-Aug-2009 07:57
Extending m.timmermans's solution, you can use the following code as a search expression parser:

<?php
$search_expression
= "apple bear \"Tom Cruise\" or 'Mickey Mouse' another word";
$words = preg_split("/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|" . "[\s,]*'([^']+)'[\s,]*|" . "[\s,]+/", $search_expression, 0, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE);
print_r($words);
?>

The result will be:
Array
(
    [0] => apple
    [1] => bear
    [2] => Tom Cruise
    [3] => or
    [4] => Mickey Mouse
    [5] => another
    [6] => word
)

1. Accepted delimiters: white spaces (space, tab, new line etc.) and commas.

2. You can use either simple (') or double (") quotes for expressions which contains more than one word.
wf
28-May-2009 04:36
Spacing out your CamelCase using preg_replace:

<?php

function spacify($camel, $glue = ' ') {
    return
preg_replace( '/([a-z0-9])([A-Z])/', "$1$glue$2", $camel );
}

echo
spacify('CamelCaseWords'), "\n"; // 'Camel Case Words'
echo spacify('camelCaseWords'), "\n"; // 'camel Case Words'

?>
chris AT cmbuckley DOT co DOT uk
27-May-2009 10:11
Here's a helpful function to space out your CamelCase using preg_split:

<?php

function spacify($camel, $glue = ' ') {
    return
$camel[0] . substr(implode($glue, array_map('implode', array_chunk(preg_split('/([A-Z])/',
       
ucfirst($camel), -1, PREG_SPLIT_NO_EMPTY | PREG_SPLIT_DELIM_CAPTURE), 2))), 1);
}

echo
spacify('CamelCaseWords'); // 'Camel Case Words'
echo spacify('camelCaseWords'); // 'camel Case Words'

?>
kenorb at gmail dot com
23-May-2009 02:56
If you need convert function arguments without default default values and references, you can try this code:

<?php
    $func_args
= '$node, $op, $a3 = NULL, $form = array(), $a4 = NULL'
   
$call_arg = preg_match_all('@(?<func_arg>\$[^,= ]+)@i', $func_args, $matches);
   
$call_arg = implode(',', $matches['func_arg']);
?>
Result: string = "$node,$op,$a3,$form,$a4"
bit_kahuna at yahoo dot com
27-Mar-2009 07:02
how to display a shortened text string with an elipsis, but on word boundaries only.

<?php
function truncate($string, $max = 70, $rep = '...') {

   
$words = preg_split("/[\s]+/", $string);
   
   
$newstring = '';
   
$numwords = 0;
   
    foreach (
$words as $word) {
        if ((
strlen($newstring) + 1 + strlen($word)) < $max) {
           
$newstring .= ' '.$word;
            ++
$numwords;
        } else {
            break;
        }
    }

    if (
$numwords < count($words)) {
       
$newstring .= $rep;
    }
   
    return
$newstring;
}
?>

hope this helps someone!  thanks for all the help from everyone else!!
csaba at alum dot mit dot edu
17-Mar-2009 09:06
If the task is too complicated for preg_split, preg_match_all might come in handy, since preg_split is essentially a special case.

I wanted to split a string on a certain character (asterisk), but only if it wasn't escaped (by a preceding backslash).  Thus, I should ensure an even number of backslashes before any asterisk meant as a splitter.  Look-behind in a regular expression wouldn't work since the length of the preceding backslash sequence can't be fixed.  So I turned to preg_match_all:

<?php
// split a string at unescaped asterisks
// where backslash is the escape character
$splitter = "/\\*((?:[^\\\\*]|\\\\.)*)/";
preg_match_all($splitter, "*$string", $aPieces, PREG_PATTERN_ORDER);
$aPieces = $aPieces[1];

// $aPieces now contains the exploded string
// and unescaping can be safely done on each piece
foreach ($aPieces as $idx=>$piece)
 
$aPieces[$idx] = preg_replace("/\\\\(.)/s", "$1", $piece);
?>
anajilly
17-Jul-2008 08:17
<?php
$s
= '<p>bleh blah</p><p style="one">one two three</p>';

$htmlbits = preg_split('/(<p( style="[-:a-z0-9 ]+")?>|<\/p>)/i', $s, -1, PREG_SPLIT_DELIM_CAPTURE);

print_r($htmlbits);
?>

Array
(
    [0] =>
    [1] => <p>
    [2] => bleh blah
    [3] => </p>
    [4] =>
    [5] => <p style="one">
    [6] =>  style="one"
    [7] => one two three
    [8] => </p>
    [9] =>
)

two interesting bits:

1. When using PREG_SPLIT_DELIM_CAPTURE, if you use more than one pair of parentheses, the result array can have members representing all pairs.  See array indexes 5 and 6 to see two adjacent delimiter results in which the second is a subset match of the first.

2. If a parenthesised sub-expression is made optional by a following question mark (ex: '/abc (optional subregex)?/') some split delimiters may be captured in the result while others are not.  See array indexes 1 and 2 to see an instance where the overall match succeeded and returned a delimiter while the optional sub-expression '( style="[-:a-z0-9 ]+")?' did not match, and did not return a delimiter.  This means it's possible to have a result with an unpredictable number of delimiters in the result array.

This second aspect is true irrespective of the number of pairs of parentheses in the regex.  This means: in a regular expression with a single optional parenthesised sub-expression, the overall expression can match without generating a corresponding delimiter in the result.
m dot timmermans at home dot NOSPAM dot nl
29-May-2008 10:56
For people who want to use the double quote to group words/fields, kind of like CSV does, you can use the following expression:
<?php
$keywords
= preg_split( "/[\s,]*\\\"([^\\\"]+)\\\"[\s,]*|[\s,]+/", "textline with, commas and \"quoted text\" inserted", 0, PREG_SPLIT_DELIM_CAPTURE );
?>
Which will result in:
Array
(
    [0] => textline
    [1] => with
    [2] => commas
    [3] => and
    [4] => quoted text
    [5] => inserted
)
crispytwo at yahoo dot com
04-Sep-2007 08:29
I was having trouble getting the PREG_SPLIT_DELIM_CAPTURE flag to work because I missed reading the "parenthesized expression" in the documentation :-( 

So the pattern should look like:
/(A)/
not just
/A/
and it works as described/expected.
Steve
23-Mar-2005 04:41
preg_split() behaves differently from perl's split() if the string ends with a delimiter. This perl snippet will print 5:

my @a = split(/ /, "a b c d e ");
print scalar @a;

The corresponding php code prints 6:

<?php print count(preg_split("/ /", "a b c d e ")); ?>

This is not necessarily a bug (nowhere does the documentation say that preg_split() behaves the same as perl's split()) but it might surprise perl programmers.
jetsoft at iinet.net.au
25-Sep-2004 03:01
To clarify the "limit" parameter and the PREG_SPLIT_DELIM_CAPTURE option,

<?php
$preg_split
('(/ /)', '1 2 3 4 5 6 7 8', 4 ,PREG_SPLIT_DELIM_CAPTURE );
?>

returns:

('1', ' ', '2', ' ' , '3', ' ', '4 5 6 7 8')

So you actually get 7 array items not 4
dave at codewhore dot org
29-May-2002 07:01
The above description for PREG_SPLIT_OFFSET_CAPTURE may be a bit confusing.

When the flag is or'd into the 'flags' parameter of preg_split, each match is returned in the form of a two-element array. For each of the two-element arrays, the first element is the matched string, while the second is the match's zero-based offset in the input string.

For example, if you called preg_split like this:

preg_split('/foo/', 'matchfoomatch', -1, PREG_SPLIT_OFFSET_CAPTURE);

it would return an array of the form:

Array(
  [0] => Array([0] => "match", [1] => 0),
  [1] => Array([1] => "match", [1] => 8)
)

Note that or'ing in PREG_DELIM_CAPTURE along with PREG_SPLIT_OFFSET_CAPTURE works as well.

POSIX Regex> <preg_replace
Last updated: Fri, 24 Jul 2009
 
 
show source | credits | sitemap | contact | advertising | mirror sites