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|arrayReference:
                The str_replace() reference
            Share "How to replace spaces with dashes in PHP?"
                
            Related snippets:
                    - Remove whitespace from the beginning and end of a string in PHP
- Locate a substring within a string in PHP
- Process CSV line in PHP
- Extract a substring from a string in PHP
- Split a string into words in PHP
- Replace spaces with dashes in PHP
- Replace only the first occurrence of a string in PHP
- Replace last occurrence of a string in a string in PHP
- Remove all attributes from HTML tags in PHP
- Remove HTML tags from a string except p, b, i and br in PHP
- Remove HTML tags from a string in PHP
Tags:
                    replace, space, dash, hyphen, string, text, all, php                Technical term:
                    Replace spaces with dashes in PHP