Skip to content

Core\Str::after

Core\Str::after(string $s, string $needle, {last?: bool}): ?stringCore\Str::after(
    string $s,
    string $needle,
    {last?: bool},
): ?string

Returns the part of a string that comes after a separator, without the separator itself.

Core\Str::after looks for the first place the separator appears and returns everything to its right. If the separator is not in the string, the result is null. If the separator is at the very end, the result is an empty string. So you can always tell "not found" apart from "found, with nothing after it". With the option { last: true }, it cuts at the last place the separator appears.

The search is case-sensitive: "Name" does not find "name". Core\Str::before does the same for the part on the left.

The examples below read a value out of a line, take a file's extension with { last: true }, and read the value out of an HTTP header.

Parameters

  • $sstringThe string to cut.
  • $needlestringThe separator to cut at. The search is case-sensitive. The needle is not part of the result.

Options

  • lastbooloptionalWhen true, cuts at the last needle. The default is false, which cuts at the first one.

Return value

Returns ?string. The text after the needle. If the needle is not in $s, the result is null. If the needle is at the end, the result is an empty string.

Examples

Read a value

<?nvs
// Shows how to take the part of a line that comes after a separator.
// `Core\Str::after` finds the first ": " and returns the text after it.
// This prints `Ada Lovelace`.
echo Core\Str::after('name: Ada Lovelace', ': ') ?? '(none)', "\n";
// Only the first ": " counts, so the second one stays in the result.
// This prints `10:30: meeting`.
echo Core\Str::after('time: 10:30: meeting', ': ') ?? '(none)', "\n";
// The separator is at the end, so the result is an empty string.
// This prints `[]`.
echo '[', Core\Str::after('name: ', ': ') ?? '(none)', "]\n";
// The separator is not in the string, so the result is `null`.
// This prints `(none)`.
echo Core\Str::after('no separator here', ': ') ?? '(none)', "\n";
Output
Ada Lovelace
10:30: meeting
[]
(none)

File extension

<?nvs
// Shows how `{ last: true }` cuts at the last separator, to read a file's extension.
array<string> $files = ['report.pdf', 'archive.tar.gz', 'README'];
foreach ($files as string $file) {
// Without the option, "archive.tar.gz" gives `tar.gz`.
?string $first = Core\Str::after($file, '.');
// With `{ last: true }`, it gives `gz`.
?string $last = Core\Str::after($file, '.', { last: true });
// "README" has no dot, so both results are `null`.
echo $file, ': ', $first ?? '(none)', ' / ', $last ?? '(none)', "\n";
}
Output
report.pdf: pdf / pdf
archive.tar.gz: tar.gz / gz
README: (none) / (none)

Read a header parameter

<?nvs
// Shows how to read the boundary out of a `Content-Type` header, as a program
// does before it splits a file upload into its parts.
array<string> $headers = [
'multipart/form-data; boundary=----form42',
'application/json',
];
foreach ($headers as string $header) {
?string $boundary = Core\Str::after($header, 'boundary=');
// A header without a boundary gives `null`, so the program can stop early.
if ($boundary == null) {
echo $header, ": not a file upload\n";
continue;
}
// This prints `----form42`.
echo $header, ': boundary is ', $boundary, "\n";
}
Output
multipart/form-data; boundary=----form42: boundary is ----form42
application/json: not a file upload