Laravel Tutorials

Convert Special Characters to ASCII with Laravel's Str::transliterate Method

Published
Convert Special Characters to ASCII with Laravel's Str::transliterate Method image

Laravel's Str::transliterate method provides a simple solution for normalizing text content by converting special characters to their ASCII equivalents.

Convert special characters to ASCII:

use Illuminate\Support\Str;
 
$content = Str::transliterate('Café Ñoño & Résumé');
// Result: 'Cafe Nono & Resume'

Here's how you might use it in a content processing service:

class ContentProcessor
{
public function processFormData(array $data)
{
return collect($data)->map(function ($value) {
return is_string($value)
? Str::transliterate($value)
: $value;
})->all();
}
 
public function createSlug(string $title)
{
return Str::slug(Str::transliterate($title));
}
 
public function normalizeSearchQuery(string $query)
{
return strtolower(Str::transliterate($query));
}
 
public function sanitizeKeywords(array $keywords)
{
return array_map(function ($keyword) {
return Str::transliterate($keyword);
}, $keywords);
}
}
 
class ArticleController extends Controller
{
public function create(Request $request, ContentProcessor $processor)
{
$processedData = $processor->processFormData([
'title' => $request->title,
'content' => $request->content,
'keywords' => $request->keywords
]);
 
Article::create($processedData);
}
}

The transliterate method makes it easy to ensure consistent text formatting across your application.

Harris Raftopoulos photo

Senior Software Engineer • Staff & Educator @ Laravel News • Co-organizer @ Laravel Greece Meetup

Sponsored

serpapi logo
SerpApi

The Web Search API for Your LLM and AI Applications

Visit SerpApi

The latest

View all →
Image Dominant Color and HEIC Support in Laravel 13.24 image

Image Dominant Color and HEIC Support in Laravel 13.24

Read article
Official Laravel Zed Extension: LSP for PHP & Blade image

Official Laravel Zed Extension: LSP for PHP & Blade

Read article
Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD image

Laravel Head: Manage Meta Tags, Open Graph, and JSON-LD

Read article
PhpStorm 2026.2 Released image

PhpStorm 2026.2 Released

Read article
Laravel Doctor: Diagnose Your App With One Artisan Command image

Laravel Doctor: Diagnose Your App With One Artisan Command

Read article
CPX: The Composer Package Executor for PHP image

CPX: The Composer Package Executor for PHP

Read article