Oihana PHP

splitOutsideQuotes.php

Table of Contents

Functions

splitOutsideQuotes()  : array<int, string>
Splits a string by a separator, ignoring separators that appear inside a quoted region.

Functions

splitOutsideQuotes()

Splits a string by a separator, ignoring separators that appear inside a quoted region.

splitOutsideQuotes(string $input, string $separator[, bool $trim = false ][, string $escape = '\\' ]) : array<int, string>

Recognised quote pairs:

  • '…' single quotes
  • "…" double quotes
  • `…` backticks
  • «…» French guillemets
  • “…” English typographic double quotes
  • ‘…’ English typographic single quotes

Inside a quoted region, a backslash (\) escapes the next byte, so \" or \\ do not terminate the region. This matches the convention used by most modern formats (JSON, shell, RFC 7230 quoted-pair). The escape itself is not decoded — it is preserved verbatim in the output segment. Pass an empty $escape to disable escape handling.

Unclosed quoted regions are tolerated: the remainder of the string is returned as the final segment.

Parameters
$input : string

The string to split.

$separator : string

The separator. An empty separator returns [$input].

$trim : bool = false

Whether to trim whitespace around each segment (default: false).

$escape : string = '\\'

The byte that escapes the next byte inside a quoted region. Default \. Pass '' to disable.

Tags
example
splitOutsideQuotes('a;b;c', ';');                 // ['a', 'b', 'c']
splitOutsideQuotes('a; "b;c"; d', ';', true);     // ['a', '"b;c"', 'd']
splitOutsideQuotes('"a\"b";c', ';');              // ['"a\"b"', 'c']
splitOutsideQuotes('a«b;c»d;e', ';');             // ['a«b;c»d', 'e']
splitOutsideQuotes('lonely', ';');                // ['lonely']
splitOutsideQuotes('"unclosed; rest', ';');       // ['"unclosed; rest']
author

Marc Alcaraz

since
1.0.8
see
unquote()
isQuoted()
Return values
array<int, string>

The list of segments.

On this page

Search results