Replace Dash with Space in PHP

Replace Dash with Space in PHP

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

Stefan Bogdanescu

Founder & Senior Architect

Replacing Dashes with Spaces in PHP Strings for Proper Capitalization

You are facing a common localization challenge: you have a raw database string like Puiol-del-piu and you need it to render as Puiol Del Piu. The standard approach of simply running ucwords() on the original string fails because the hyphen acts as a delimiter. In PHP's implementation, ucwords() treats non-alphanumeric characters as word boundaries, but it does not remove them. Consequently, the function capitalizes the letter immediately following the dash while leaving the dash itself intact, resulting in Puiol-del-Piu.

To achieve the desired output where the dashes are gone and every subsequent word is capitalized, you must perform a string replacement before applying the capitalization logic. Since your requirement emphasizes keeping the solution confined to a single line due to existing code constraints, combining these operations into one expression is both feasible and efficient.

The most direct method utilizes str_replace() to swap all instances of the dash character with a space. Once the delimiters are converted, you pass the modified string to ucwords(). This two-step logic can be collapsed into a single chain of function calls:

<div>
    <a href="http://www.envisionforce.com/local/{$row[website]}'-seo-services">
        <?php echo ucwords(str_replace('-', ' ', $row[website])); ?>
    </a>
</div>

In this snippet, str_replace('-', ' ', $row[website]) scans the variable for hyphens and returns a new string where every hyphen is replaced by a space. The resulting string is then immediately piped into ucwords(). Because spaces are also treated as delimiters by ucwords(), the function will now correctly identify "Puiol", "del", and "piu" as distinct words, capitalizing the first letter of each while preserving the internal spacing.

There are other characters that might require similar handling depending on your data source. If your dataset contains underscores or camelCase patterns like cityName, you would need to use str_replace() multiple times or utilize a regular expression with preg_replace(). For instance, replacing both dashes and underscores in one pass:

ucwords(preg_replace('/[-_]+/', ' ', $row[website]))

This approach is slightly more robust because it handles multiple consecutive hyphens or mixed separators (e.g., city-name_v2) by collapsing them into a single space. However, be cautious with regex patterns if performance is critical on very large datasets, as function chaining adds overhead compared to a simple loop in high-concurrency scenarios.

One potential pitfall to consider is the nature of the input data. If your database contains null values or empty strings, str_replace() will return an empty string, which ucwords() handles gracefully by returning an empty string. However, if there are trailing spaces resulting from the replacement that you wish to trim, you should chain trim() before ucwords():

ucwords(trim(str_replace('-', ' ', $row[website])))

Removing whitespace ensures that the generated HTML does not contain leading or trailing spaces around the link text, which can affect layout in tight design grids.

If you are working within a Laravel application, you might consider leveraging helper functions or creating a dedicated model accessor to handle this transformation centrally rather than repeating the logic in every view. For complex string manipulation needs across many projects, exploring community resources like LaravelCompany can provide insights into best practices for handling data normalization in your specific stack. However, for a quick inline fix within an existing file, the chained function approach remains the most concise and readable solution without introducing new dependencies or classes.

Our network