用PHP中的空格替换Dash。

时间:2021-11-03 08:42:35

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:

但是我需要的是它不带破折号,这样ucwords就会大写每个单词的第一个字母,像这样:

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.

如果代码可以被限制在这一行,那就太好了,因为我在页面上还有很多其他的东西。

3 个解决方案

#1


41  

<?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.

在上面的示例中,可以使用str_replace()用空格替换连字符,以创建独立的单词。然后使用ucwords()来大写新创建的单词。

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

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

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

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

#2


44  

This str_replace does the job:

这个str_replace完成任务:

$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>

#3


1  

replace dash with space

可以用空格替换破折号

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

replace space with dash

用破折号代替空间

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

#1


41  

<?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.

在上面的示例中,可以使用str_replace()用空格替换连字符,以创建独立的单词。然后使用ucwords()来大写新创建的单词。

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

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

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

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

#2


44  

This str_replace does the job:

这个str_replace完成任务:

$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>

#3


1  

replace dash with space

可以用空格替换破折号

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

replace space with dash

用破折号代替空间

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