ramsey / str-begins-ends
Provides functions to test whether a string starts or ends with a certain substring.
Requires
- php: ^5.4 || ^7.0
Requires (Dev)
- jakub-onderka/php-parallel-lint: ^1
- mockery/mockery: ^0.9.11
- phpunit/phpunit: ^4.8
- squizlabs/php_codesniffer: ^3
This package is auto-updated.
Last update: 2020-10-28 03:19:37 UTC
README
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.