- Instant help with your Php coding problems

Replace spaces with dashes in PHP

Question:
How to replace spaces with dashes in PHP?
Answer:
$originalText = 'This is my original text to convert';

$newText = str_replace(' ', '-', $originalText);

echo $newText;
Description:

In PHP to replace all spaces with dashes use the str_replace() method that replaces all occurrences of the search string with the replacement string. If you don't need complex replacing rules (like regular expressions), you should use str_replace() instead of preg_replace() .

Syntax:

str_replace(
    array|string $search,
    array|string $replace,
    string|array $subject,
    int &$count = null
): string|array
Share "How to replace spaces with dashes in PHP?"