Get support for ramsey/str-begins-ends

If you're new to LTH, please see our FAQ for more information on what it is we do.

Support Options

Unfortunately, there are currently no active helpers for this repository on the platform. Until they become available, we reccomend the following actions:

View Open Issues

Take a look to see if anyone else has experienced the same issue as you and if they managed to solve it.

Open an Issue

Make sure to read any relevant guidelines for opening issues on this repo before posting a new issue.

Sponsor directly

Check out the page and see if there are any options to sponsor this project or it's developers directly.

ramsey/str-begins-ends

ramsey/str-begins-ends

Source Code Latest Version Software License PHP Version Build Status Coverage Status Total Downloads

ramsey/str-begins-ends provides functions to test whether a string begins or ends with a certain substring. This is a polyfill for functions based on Will Hudgins's PHP RFC "Add str begin and end functions."

This project adheres to a Contributor Code of Conduct. By participating in this project and its community, you are expected to uphold this code.

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require ramsey/str-begins-ends

Documentation

This library provides the following functions in the global scope. It will not cause conflicts in any project using it, should PHP decide to adopt and implement the RFC in a future version.

str_starts_with

str_starts_with(string $haystack , string $needle): bool

Performs a case-sensitive check to determine whether $haystack begins with $needle.

Example

$url = 'http://example.com';

if (str_starts_with($url, 'http://')) {
    $url = str_replace('http://', 'https://', $url);
}

str_ends_with

str_ends_with(string $haystack , string $needle): bool

Performs a case-sensitive check to determine whether $haystack ends with $needle.

Example

$file = '/path/to/something.log';

if (str_ends_with($file, '.log')) {
    $contents = file_get_contents($file);
}

str_begins_with_ci

str_begins_with_ci(string $haystack , string $needle): bool

Performs a case-insensitive check to determine whether $haystack begins with $needle.

Example

$url = 'HTTPS://example.com';

if (str_begins_with_ci($url, 'https://')) {
    $url = substr($url, 8);
}

str_ends_with_ci

str_ends_with_ci(string $haystack , string $needle): bool

Performs a case-insensitive check to determine whether $haystack ends with $needle.

Example

$file = '/path/to/something.TXT';

if (str_ends_with_ci($file, '.txt')) {
    $contents = file_get_contents($file);
}

mb_str_starts_with

mb_str_starts_with(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool

Performs a case-sensitive, multi-byte safe str_starts_with() operation to check whether $haystack begins with $needle.

This function is only available if the mbstring extension is installed.

Example

$runePoem = 'ᚠᛇᚻ᛫ᛒᛦᚦ᛫ᚠᚱᚩᚠᚢᚱ᛫ᚠᛁᚱᚪ᛫ᚷᛖᚻᚹᛦᛚᚳᚢᛗ';

if (mb_str_starts_with($runePoem, 'ᚠᛇᚻ')) {
    $poem = 'Wealth is a comfort to all men';
}

mb_str_ends_with

mb_str_ends_with(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool

Performs a case-sensitive, multi-byte safe str_ends_with() operation to check whether $haystack ends with $needle.

This function is only available if the mbstring extension is installed.

Example

$sanskrit = 'काचं शक्नोम्यत्तुम् । नोपहिनस्ति माम् ॥';

if (mb_str_ends_with($sanskrit, 'माम् ॥')) {
    $translation = 'I can eat glass';
}

mb_str_begins_with_ci

mb_str_begins_with_ci(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool

Performs a case-insensitive, multi-byte safe str_begins_with_ci() operation to check whether $haystack begins with $needle.

This function is only available if the mbstring extension is installed.

Example

$poem = 'Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ';

if (mb_str_begins_with_ci($poem, 'ΤῊ')) {
    $poet = 'Οδυσσέας Ελύτης';
}

mb_str_ends_with_ci

mb_str_ends_with_ci(string $haystack , string $needle [, string $encoding = mb_internal_encoding()]): bool

Performs a case-insensitive, multi-byte safe str_ends_with_ci() operation to check whether $haystack ends with $needle.

This function is only available if the mbstring extension is installed.

Example

$poem = 'Τὴ γλῶσσα μοῦ ἔδωσαν ἑλληνικὴ';

if (mb_str_ends_with_ci($poem, 'ἙΛΛΗΝΙΚῊ')) {
    $poet = 'Οδυσσέας Ελύτης';
}

Contributing

Contributions are welcome! Please read CONTRIBUTING for details.

Copyright and License

The ramsey/str-begins-ends library is copyright © Ben Ramsey and licensed for use under the MIT License (MIT). Please see LICENSE for more information.

Our Mission

We want to make open source more sustainable. The entire platform was born from this and everything we do is in aid of this.

Interesting Articles

Thank you for checking out LiveTechHelper |
2025 © lth-dev incorporated

p-e622a1a2