PHPackages                             ramsey/str-begins-ends - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. ramsey/str-begins-ends

Abandoned → [symfony/polyfill-php80](/?search=symfony%2Fpolyfill-php80)ArchivedLibrary[Utility &amp; Helpers](/categories/utility)

ramsey/str-begins-ends
======================

Provides functions to test whether a string starts or ends with a certain substring.

1.0.0(6y ago)536[1 issues](https://github.com/ramsey/str-begins-ends/issues)MITPHPPHP ^5.4 || ^7.0

Since Jun 19Pushed 5y agoCompare

[ Source](https://github.com/ramsey/str-begins-ends)[ Packagist](https://packagist.org/packages/ramsey/str-begins-ends)[ Docs](https://github.com/ramsey/str-begins-ends)[ RSS](/packages/ramsey-str-begins-ends/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (4)Versions (2)Used By (0)

ramsey/str-begins-ends
======================

[](#ramseystr-begins-ends)

[![Source Code](https://camo.githubusercontent.com/9a62180c0ba23f36af47725bd4aac808d5fb9166221420854987d80e0c144277/687474703a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d72616d7365792f7374722d2d626567696e732d2d656e64732d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/ramsey/str-begins-ends)[![Latest Version](https://camo.githubusercontent.com/5f68c1b18d9f3ef1cab4ad2d5fdd277cca3e98c70b3e2fef1d92d118e3b96fdb/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616d7365792f7374722d626567696e732d656e64732e7376673f7374796c653d666c61742d737175617265266c6162656c3d72656c65617365)](https://packagist.org/packages/ramsey/str-begins-ends)[![Software License](https://camo.githubusercontent.com/c1c1b257efb00a4ebcbcab017d04b10e72188069a9f0392e9c519bb82c0d25d4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f72616d7365792f7374722d626567696e732d656e64732e7376673f7374796c653d666c61742d737175617265)](https://github.com/ramsey/str-begins-ends/blob/master/LICENSE)[![PHP Version](https://camo.githubusercontent.com/89c8a4e8fff7093f9b7fc5d34d85d80770129a5710fb1b0df916bc162f5f5bd1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f72616d7365792f7374722d626567696e732d656e64732e7376673f7374796c653d666c61742d737175617265)](https://php.net)[![Build Status](https://camo.githubusercontent.com/b41c2074178349c98cf1b59bc38450af117b12ebc1dd39707f0ca91f036eb8ee/68747470733a2f2f696d672e736869656c64732e696f2f7472617669732f72616d7365792f7374722d626567696e732d656e64732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://travis-ci.org/ramsey/str-begins-ends)[![Coverage Status](https://camo.githubusercontent.com/927d1e1fd62566a384e2c7378567479dd6b8affaa399763481208b5e3a60e856/68747470733a2f2f696d672e736869656c64732e696f2f636f766572616c6c732f6769746875622f72616d7365792f7374722d626567696e732d656e64732f6d61737465722e7376673f7374796c653d666c61742d737175617265)](https://coveralls.io/r/ramsey/str-begins-ends?branch=master)[![Total Downloads](https://camo.githubusercontent.com/09115584d4ed7c66fc27f73f90c53f3eb2be93d4d6e48b36bf9d327082b53ec4/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72616d7365792f7374722d626567696e732d656e64732e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d6d656469756d76696f6c6574726564)](https://packagist.org/packages/ramsey/str-begins-ends)

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](https://wiki.php.net/rfc/add_str_begin_and_end_functions)."

This project adheres to a [Contributor Code of Conduct](https://github.com/ramsey/str-begins-ends/blob/master/.github/CODE_OF_CONDUCT.md). By participating in this project and its community, you are expected to uphold this code.

Installation
------------

[](#installation)

The preferred method of installation is via [Composer](http://getcomposer.org/). 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
-------------

[](#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](https://wiki.php.net/rfc/add_str_begin_and_end_functions) in a future version.

str\_starts\_with
-----------------

[](#str_starts_with)

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

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

### Example

[](#example)

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

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

str\_ends\_with
---------------

[](#str_ends_with)

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

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

### Example

[](#example-1)

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

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

str\_begins\_with\_ci
---------------------

[](#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

[](#example-2)

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

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

str\_ends\_with\_ci
-------------------

[](#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

[](#example-3)

```
$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)

```
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](https://www.php.net/manual/en/book.mbstring.php) is installed.

### Example

[](#example-4)

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

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

mb\_str\_ends\_with
-------------------

[](#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](https://www.php.net/manual/en/book.mbstring.php) is installed.

### Example

[](#example-5)

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

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

mb\_str\_begins\_with\_ci
-------------------------

[](#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](https://www.php.net/manual/en/book.mbstring.php) is installed.

### Example

[](#example-6)

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

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

mb\_str\_ends\_with\_ci
-----------------------

[](#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](https://www.php.net/manual/en/book.mbstring.php) is installed.

### Example

[](#example-7)

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

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

Contributing
------------

[](#contributing)

Contributions are welcome! Please read [CONTRIBUTING](https://github.com/ramsey/str-begins-ends/blob/master/.github/CONTRIBUTING.md) for details.

Copyright and License
---------------------

[](#copyright-and-license)

The ramsey/str-begins-ends library is copyright © [Ben Ramsey](https://benramsey.com)and licensed for use under the MIT License (MIT). Please see [LICENSE](https://github.com/ramsey/str-begins-ends/blob/master/LICENSE) for more information.

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance16

Infrequent updates — may be unmaintained

Popularity12

Limited adoption so far

Community6

Small or concentrated contributor base

Maturity54

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

2516d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/f514481fa9cfdfd5dbcaccb3e5b480f18ea05ebf331cddf9b1c5a2a96a78fef5?d=identicon)[ramsey](/maintainers/ramsey)

---

Top Contributors

[![ramsey](https://avatars.githubusercontent.com/u/42941?v=4)](https://github.com/ramsey "ramsey (10 commits)")

---

Tags

phppolyfillstr-beginsstr-endsstringspolyfillstringsmb\_str\_beginsmb\_str\_endsmb\_str\_ibeginsmb\_str\_iendsstr\_beginsstr\_endsstr\_ibeginsstr\_iends

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

![Health badge](/badges/ramsey-str-begins-ends/health.svg)

```
[![Health](https://phpackages.com/badges/ramsey-str-begins-ends/health.svg)](https://phpackages.com/packages/ramsey-str-begins-ends)
```

###  Alternatives

[doctrine/inflector

PHP Doctrine Inflector is a small library that can perform string manipulations with regard to upper/lowercase and singular/plural forms of words.

11.4k855.8M711](/packages/doctrine-inflector)[symfony/polyfill-mbstring

Symfony polyfill for the Mbstring extension

7.8k1.2B515](/packages/symfony-polyfill-mbstring)[paragonie/random_compat

PHP 5.x polyfill for random\_bytes() and random\_int() from PHP 7

8.2k655.0M404](/packages/paragonie-random-compat)[symfony/polyfill-ctype

Symfony polyfill for ctype functions

4.0k982.0M125](/packages/symfony-polyfill-ctype)[symfony/polyfill-php72

Symfony polyfill backporting some PHP 7.2+ features to lower PHP versions

4.8k674.7M31](/packages/symfony-polyfill-php72)[symfony/polyfill-intl-idn

Symfony polyfill for intl's idn\_to\_ascii and idn\_to\_utf8 functions

3.4k774.6M90](/packages/symfony-polyfill-intl-idn)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
