Convert spaces to dash and lowercase with PHP

Convert spaces to dash and lowercase with PHP

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Convert Spaces to Dashes and Lowercase in PHP

You are currently trying to combine strtolower() and str_replace(), but the syntax in your attempt prevents either function from executing correctly. The line <code>print (str_replace(' ', '-', $string)strtolower($blob));</code> fails because you are attempting to chain two standalone functions directly together without an intermediate step. In PHP, strtolower($blob) returns a modified string, not a function object that can be immediately piped into str_replace. You must assign the result of the first operation to a variable or wrap the entire logic in parentheses that handle the order of operations explicitly.

To achieve this in a single line, you need to nest the functions so that the transformation happens sequentially. The most straightforward approach is to pass the output of one function directly as an argument to the next, provided the nesting is syntactically valid.

<?php
$slug = str_replace(' ', '-', strtolower($blob));
print $slug;
?>

This single statement performs the case conversion first, ensuring that any uppercase letters are processed before the replacement occurs. If you reverse the order—replacing spaces first and then lowercasing—the result is technically identical regarding the final characters, but doing the case conversion first is often more efficient if you anticipate using regex later, where case-insensitive flags might be relevant.

However, relying solely on str_replace(' ', '-', ...) introduces a common pitfall: multiple consecutive spaces will become multiple consecutive dashes. If your input string is <code>"hello world"</code>, the result becomes <code>"hello---world"</code>. For URL slugs or database identifiers, this is usually undesirable. You likely want to normalize whitespace so that any amount of spacing results in a single hyphen.

To handle variable whitespace robustly, you should look toward regular expressions using preg_replace. The pattern /[\s]+/ matches one or more whitespace characters (spaces, tabs, newlines), and replacing them with a single dash ensures clean output regardless of how much padding exists in the original $blob.

<?php
$slug = str_replace('.', '-', preg_replace('/\s+/', '-', strtolower($blob)));
print $slug;
?>

This example also demonstrates handling periods, which are often converted to dashes in slug generation logic. You might also want to remove trailing or leading spaces before applying the transformation to avoid generating a slug that starts with a dash (e.g., <code>"-hello-world"</code>). A common pattern involves stripping whitespace first using trim().

<?php
$slug = str_replace('.', '-', preg_replace('/\s+/', '-', strtolower(trim($blob))));
print $slug;
?>

While chaining functions like this keeps the code concise, readability can suffer if the line becomes too complex. If you find yourself nesting too many operations, consider using a helper function or breaking it down into distinct steps for maintainability.

If you are working within a Laravel application, you might prefer utilizing the Str facade, which provides fluent methods that can make chaining slightly more readable. The slug method in Laravel is specifically designed to handle exactly this scenario: converting spaces to dashes and lowercasing automatically.

<?php
use Illuminate\Support\Str;

$slug = Str::slug(strtolower($blob));
print $slug;
?>

The Str::slug() method internally handles the whitespace normalization, making your code less prone to regex errors or edge cases with multiple spaces. For larger projects, leveraging these built-in tools reduces boilerplate and minimizes the risk of introducing subtle bugs. If you need custom behavior beyond what the framework provides, sticking to the preg_replace pattern remains a reliable, low-overhead solution that works consistently across different PHP versions without requiring external dependencies.

Our network