Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace Dash with Space in PHP

Tags:

string

php

I currently have this line in my code:

<div><a href="http://www.envisionforce.com/local/'.$row[website].'-seo-services">'.ucwords($row[website]).'</a></div> 

And it will display a city name such as this:

Puiol-del-piu 

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.

like image 579
user1848777 Avatar asked Nov 24 '12 23:11

user1848777


People also ask

How to replace a Space with hyphen in a string in PHP?

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

How do you replace spaces with dashes?

To replace the spaces with dashes in a string, call the replaceAll() method on the string, e.g. str. replaceAll(' ', '-') . The replaceAll method will return a new string where all spaces are replaced by dashes.

How to convert Space into in PHP?

The return value of strtolower can be passed as the third argument to str_replace (where $string is present). The str_replace function is used to replace a set of characters/character with a different set of character/string.


2 Answers

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="http://www.envisionforce.com/local/'.$row[website].'-seo-services">'.ucwords($varcity).'</a></div> 
like image 146
Genesis Avatar answered Sep 16 '22 15:09

Genesis


<?php echo '<div><a href="http://www.envisionforce.com/local/'.$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.

http://php.net/manual/en/function.str-replace.php

http://php.net/manual/en/function.ucwords.php

like image 29
Samuel Cook Avatar answered Sep 20 '22 15:09

Samuel Cook