marjovanlier/stringmanipulation

A PHP library for efficient string manipulation, focusing on data normalisation, encoding conversion and validation.

v1.0.79 2024-03-11 07:02 UTC

README

Table of Contents

Introduction

Welcome to the StringManipulation library, a robust and efficient PHP toolkit designed to enhance string handling in your PHP projects. With its user-friendly interface and performance-oriented design, this library is an essential addition for developers looking to perform complex string manipulations with ease.

Packagist Version Packagist Downloads Packagist License PHP from Packagist Latest Stable PHPStan Enabled Phan Enabled Psalm Enabled codecov Qodana

Features

  • Search Words: Transform strings into a search-optimized format for database queries, removing unnecessary characters and optimizing for search engine algorithms.
  • Name Fix: Standardize last names by capitalizing the first letter of each part of the name and handling prefixes correctly, ensuring consistency across your data.
  • UTF-8 to ANSI: Convert UTF-8 encoded characters to their ANSI equivalents, facilitating compatibility with systems that do not support UTF-8.
  • Remove Accents: Strip accents and special characters from strings to normalize text, making it easier to search and compare.
  • Date Validation: Ensure date strings conform to specified formats and check for logical consistency, such as correct days in a month.
  • Time Part Validation: Validate the time components within date strings for accuracy, ensuring that hours, minutes, and seconds are within valid ranges.

Installation

Install the package via Composer with the following command:

composer require marjovanlier/stringmanipulation

Usage

For more detailed examples of each feature, please refer to the corresponding sections below.

use MarjovanLier\StringManipulation\StringManipulation;

$result = StringManipulation::searchWords('Hello_World');
echo $result; // Outputs: 'hello world'

License

This library is licensed under the MIT License. For more information, please refer to the License File.

Detailed Examples

Name Standardization

  • Case Conversion: Easily convert strings between upper case, lower case, and title case, allowing for flexible text formatting and presentation. For example, converting 'john doe' to 'John Doe' for proper name presentation.
use MarjovanLier\StringManipulation\StringManipulation;

$fixedName = StringManipulation::nameFix('mcdonald');
echo $fixedName; // Outputs: 'McDonald'

UTF-8 to ANSI Conversion

use MarjovanLier\StringManipulation\StringManipulation;

$ansiString = StringManipulation::utf8Ansi('Äpfel');
echo $ansiString; // Outputs: 'Äpfel' in ANSI format

Search Words

This feature optimizes strings for database queries by removing unnecessary characters and optimizing for search engine algorithms.

use MarjovanLier\StringManipulation\StringManipulation;

$result = StringManipulation::searchWords('Hello_World');
echo $result; // Outputs: 'hello world'

Name Fix

Standardize last names by capitalizing the first letter of each part of the name and handling prefixes correctly.

use MarjovanLier\StringManipulation\StringManipulation;

$fixedName = StringManipulation::nameFix('de souza');
echo $fixedName; // Outputs: 'De Souza'

UTF-8 to ANSI Conversion

Convert UTF-8 encoded characters to their ANSI equivalents, facilitating compatibility with systems that do not support UTF-8.

use MarjovanLier\StringManipulation\StringManipulation;

$ansiString = StringManipulation::utf8Ansi('Über');
echo $ansiString; // Outputs: 'Uber' in ANSI format

Remove Accents

Strip accents and special characters from strings to normalize text, making it easier to search and compare.

use MarjovanLier\StringManipulation\StringManipulation;

$normalizedString = StringManipulation::removeAccents('Crème Brûlée');
echo $normalizedString; // Outputs: 'Creme Brulee'

Date Validation

Ensure date strings conform to specified formats and check for logical consistency, such as correct days in a month.

use MarjovanLier\StringManipulation\StringManipulation;

$isValidDate = StringManipulation::validateDate('2023-02-29');
echo $isValidDate ? 'Valid' : 'Invalid'; // Outputs: 'Invalid'

Time Part Validation

Validate the time components within date strings for accuracy, ensuring that hours, minutes, and seconds are within valid ranges.

use MarjovanLier\StringManipulation\StringManipulation;

$isValidTime = StringManipulation::validateTime('25:61:00');
echo $isValidTime ? 'Valid' : 'Invalid'; // Outputs: 'Invalid'

Advanced Usage

For more complex string manipulations, consider chaining functions to achieve unique transformations. For instance, you could first normalise a string, apply a search optimisation, and finally standardise the casing for a comprehensive text processing example.

use MarjovanLier\StringManipulation\StringManipulation;

$originalString = 'Crème Brûlée';
$processedString = StringManipulation::nameFix(StringManipulation::utf8Ansi(StringManipulation::removeAccents($originalString)));
echo $processedString; // Outputs: 'Creme Brulee'

This approach allows for flexible and powerful string manipulations by combining the library's functions to suit your specific needs.

Contributing

We welcome contributions to the StringManipulation library! If you're interested in helping, please follow these steps:

  1. Fork the repository and create your feature branch.
  2. Ensure your changes adhere to our coding standards and include tests if applicable.
  3. Submit a pull request with a detailed description of your changes.

Thank you for your interest in improving our library!

Testing

To ensure the reliability and functionality of your string manipulations, it's recommended to run the entire test suite with the following command:

./vendor/bin/phpunit

To run specific tests or test suites, you can use PHPUnit flags to filter tests. For example, to run tests in a specific file:

./vendor/bin/phpunit --filter testFileName

And to run tests matching a specific name pattern:

./vendor/bin/phpunit --filter '/::testNamePattern$/'

System Requirements

  • PHP 8.2 or later.

Support

For support, please open an issue on our GitHub repository.