Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
766 views
in Technique[技术] by (71.8m points)

string - Replace Dash with Space in 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.

question from:https://stackoverflow.com/questions/13546597/replace-dash-with-space-in-php

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
<?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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...