removeAccents()

Strips accents and special characters from strings to normalise text.

Table of contents

  1. Signature
  2. Parameters
  3. Returns
  4. Description
    1. Performance
    2. Character Coverage
  5. Examples
    1. Basic Usage
    2. International Text
    3. Special Characters
  6. Use Cases
    1. Search Index Generation
    2. URL Slug Creation
  7. Related Methods

Signature

public static function removeAccents(string $str): string

Parameters

Parameter Type Description
$str string The input string containing accented characters

Returns

string - The input string with all accents and diacritics removed.


Description

The removeAccents() method efficiently strips accents and diacritical marks from strings, making text easier to search, compare, and index. This is essential for:

  • Search functionality - Match “cafe” when users search for “cafe”
  • URL slugs - Generate clean URLs from titles
  • Data normalisation - Standardise text for comparison

Performance

  • ~450,000 operations per second
  • Uses hash table lookups with strtr() for O(1) character replacement
  • Static caching of character mapping tables

Character Coverage

Supports 266+ accented and special characters including:

  • Latin characters (a, e, i, o, u with various diacritics)
  • Extended Latin (Ae, Oe, ss, etc.)
  • Greek characters
  • Currency symbols
  • Special typographic characters

Examples

Basic Usage

use MarjovanLier\StringManipulation\StringManipulation;

$result = StringManipulation::removeAccents('Creme Brulee');
echo $result; // Output: Creme Brulee

International Text

// French
StringManipulation::removeAccents('francais');
// Output: francais

// German
StringManipulation::removeAccents('Munchen Ubung');
// Output: Munchen Ubung

// Spanish
StringManipulation::removeAccents('Espanol manana');
// Output: Espanol manana

// Portuguese
StringManipulation::removeAccents('Sao Pauloacao');
// Output: Sao Paulo acao

Special Characters

// Ligatures
StringManipulation::removeAccents('AEsop OEuvre');
// Output: AEsop OEuvre

// Nordic characters
StringManipulation::removeAccents('Malmo Oslo');
// Output: Malmo Oslo

// Eastern European
StringManipulation::removeAccents('Praha Lodz');
// Output: Praha Lodz

Use Cases

Search Index Generation

function generateSearchIndex(string $text): string
{
    return strtolower(
        StringManipulation::removeAccents($text)
    );
}

$title = "Cafe Creme - Munchen";
$searchIndex = generateSearchIndex($title);
// Result: cafe creme - munchen

URL Slug Creation

function createSlug(string $title): string
{
    $normalised = StringManipulation::removeAccents($title);
    $lowercase = strtolower($normalised);
    $slug = preg_replace('/[^a-z0-9]+/', '-', $lowercase);
    return trim($slug, '-');
}

$slug = createSlug('Cafe Creme Brulee');
// Result: cafe-creme-brulee


Back to top

Copyright © 2024 Marjo Wenzel van Lier. Distributed under the MIT License.