• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • VBA
  • About
You are here: Home / PHP / Replace Space with Dash in php String

Replace Space with Dash in php String

March 15, 2022 Leave a Comment

To replace a space with a dash in php, the easiest way is to use the php str_replace() function.

$string_with_spaces = "This is a string.";

$string_with_dashes = str_replace(" ","-", $string_with_spaces);

echo $string_with_dashes;

// Output:
This-is-a-string.

You can also use the preg_replace() function to use regex to replace spaces with dashes in php.

$string_with_spaces = "This is a string.";

$string_with_dashes = preg_replace("/\s+/","-", $string_with_spaces);

echo $string_with_dashes;

// Output:
This-is-a-string.

When working with strings in php, being able to manipulate your variables easily is important. There are a number of built in string methods which allow us to get information and change string variables.

One such function which is very useful is the string str_replace() function. With str_replace(), we can create a new string where the specified value is replaced by another specified value.

We can use the str_replace() function to replace the spaces in a string with dashes.

To replace spaces with dashes, we can use the str_replace() function as shown in the following php code.

$string_with_spaces = "This is a string.";

$string_with_dashes = str_replace(" ","-", $string_with_spaces);

echo $string_with_dashes;

// Output:
This-is-a-string.

Replacing Spaces with Underscores in php with preg_replace()

Another function we can use to replace spaces with underscores in a php string is the php preg_replace() function.

preg_replace() performs a regular expression (regex) search on a string or array of strings, and returns a string or an array of strings where all matches of the regex pattern or list of regex patterns found are replaced with substrings.

Below is an example of how to replace spaces with dashes using preg_replace() in php.

$string_with_spaces = "This is a string.";

$string_with_dashes = preg_replace("/\s+/","-", $string_with_spaces);

echo $string_with_dashes;

// Output:
This-is-a-string.

Using the str_replace() function to Make Replacements in Strings in php

Below are a few more examples of how you can use the str_replace() function to make replacements in strings in php.

For example, if we want to replace underscores with spaces, we can do the following.

$string_with_underscores = "This_is_a_string.";

$string_with_spaces = str_replace("_"," ", $string_with_underscores);

echo $string_with_spaces ;

// Output:
This is a string.

If we want to replace all the spaces with periods, we can do so easily in the following php code.

$string_with_spaces = "This is a string.";

$string_with_periods = str_replace(" ",".", $string_with_spaces);

echo $string_with_periods;

// Output:
This_is_a_string.

You can also replace full words with other words. Let’s replace the word “a” with “the”.

$string_with_spaces = "This is a string.";

$string_with_the = str_replace("a","the", $string_with_spaces);

echo $string_with_the;

// Output:
This is the string.

Hopefully this article has been useful for you to learn how to replace spaces with dashes in php.

Other Articles You'll Also Like:

  • 1.  php switch case – How to Use Switch…Case Statements in php
  • 2.  Get User Agent in php with $_SERVER[‘HTTP_USER_AGENT’]
  • 3.  php session_destroy() – Delete Session File Where Session Data is Stored
  • 4.  Using strtolower() in php to Convert String to Lowercase
  • 5.  php array_walk() – Modify Elements in Array with Callback Function
  • 6.  Remove First Character from String Using php
  • 7.  Add Months to Date in php
  • 8.  php is_array() Function – Check if Variable is Array in php
  • 9.  php array_fill() – Create Array of Length N with Same Value
  • 10.  php atan2 – Find Arctangent of the Quotient of Two Numbers

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2022 · The Programming Expert · About · Privacy Policy