Replace Dash with Space in PHP

php replace space and dash with underscore
php replace space with underscore
php replace special characters with dash
replace comma with space in php
php replace string in file
php remove spaces and dashes from string
replace with comma in php
how to replace space with hyphen in url in php

I currently have this line in my code:

<div><a href="'.$row[website].'-seo-services">'.ucwords($row[website]).'</a></div>

And it will display a city name such as this:


But what I need is for it to display without the dashes and so that ucwords will capitalize the first letter of each word such as this:

Puiol Del Piu

It would be great if the code could be confined to this one line because I have a lot more going on with others stuff in the page as well.

echo '<div><a href="'.$row[website].'-seo-services">'.ucwords(str_replace("-"," ",$row[website])).'</a></div>';

In the above example you can use str_replace() to replace hyphens with spaces to create separate words. Then use ucwords() to capitalize the newly created words.

Replace spaces with a dash in a URL, 2 Answers. You can use preg_replace: preg_replace('/[[:space:]]+/', '-', $subject); This will replace all instances of space with a single '-' dash. How to Replace space with Dash in PHP. It is very common requirement to do a task where you need to Replace space with Dash in PHP. PHP has a function str_replace() function which takes 3 parameters: i) look for that character. ii) replace with the character. iii) source string.

This str_replace does the job:

$string = str_replace("-", " ", $string);

Also, you can make it as a function.

function replace_dashes($string) {
    $string = str_replace("-", " ", $string);
    return $string;

Then you call it:

$varcity = replace_dashes($row[website]);
<div><a href="'.$row[website].'-seo-services">'.ucwords($varcity).'</a></div>

Replace hyphen (-) with space( ) in php, Replace hyphen (-) with space( ) in php. Posted 1 year ago by Dev0ps. $string = "​Within 3 days of-death the enzymes-that once-digested  Replace blank space with Hyphen/Dash inside string in PHP.Remove blank white space between string words and overlap them with hyphen using PHP.

replace dash with space

str_replace("-"," ",$row[text])

replace space with dash

str_replace(" ","-",$row[text])

Replace Dash (-) and Underscore ( _ ) with Space, I want to replace dashes & underscores with space in this image file name: code is part of the Advanced Image Grabber plugin ( in class_image_grabber.​php ) Teams. Q&A for Work. Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information.

How to Replace space with Dash in PHP, Read more on how to Replace space with Dash in PHP. PHP has a function str_replace() function which takes 3 parameters: i) look for that  I have a PHP Script that users will enter a name like: Alex_Newton, However, some users will use a space rather than an underscore, so my question is: How do I auto-replace spaces with Underscore

Replace hyphen (-) with comma (,) in php, To remove the first space before the comma simply include a whitespace before the - in your str_replace():. <?php $string = "Nov 07 ‐ 08 & 14 ‐  If search and replace are arrays, then str_replace() takes a value from each array and uses them to search and replace on subject. If replace has fewer values than search , then an empty string is used for the rest of replacement values.

Replace Whitespace with Hyphen - PHP, Hi all, how can I replace white-space with a hyphen AND also strip all alphanumeric characters? I've managed to replace all white-space. Don't really want to  The trim () function removes whitespace and other predefined characters from both sides of a string. ltrim () - Removes whitespace or other predefined characters from the left side of a string. rtrim () - Removes whitespace or other predefined characters from the right side of a string. trim ( string,charlist ) Parameter Values.

  • "one line" Use functions!
  • This regex do the job. $string = preg_replace("/[\-]/", " ", $string);
  • No need for a regex - str_replace will do the job for less memory, cpu and elapsed time
  • The first two params in your example are swapped from what they should be. Easy fix! Thanks for the examples.
  • @SteveMeisner, actually not. He want to get rid of the "dashes", replacing them with spaces. That's what the function does.
  • Darkeden — you're correct! I read it opposite (probably because I was trying to replace spaces with dashes...). Thanks!