在PHP中用空格替换Dash

use*_*777 27 php string

我目前在我的代码中有这一行:

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

它会显示一个城市名称,例如:

Puiol-del-piu
Run Code Online (Sandbox Code Playgroud)

但我需要的是它显示没有破折号,以便ucwords将大写每个单词的第一个字母,如下所示:

Puiol Del Piu
Run Code Online (Sandbox Code Playgroud)

如果代码可以局限于这一行,那将是很好的,因为我在页面中还有更多的东西与其他东西相关.

Dar*_*den 52

这个str_replace做的工作:

$string = str_replace("-", " ", $string);
Run Code Online (Sandbox Code Playgroud)

此外,您可以将其作为一个功能.

function replace_dashes($string) {
    $string = str_replace("-", " ", $string);
    return $string;
}
Run Code Online (Sandbox Code Playgroud)

然后你称之为:

$varcity = replace_dashes($row[website]);
<div><a href="http://www.envisionforce.com/local/'.$row[website].'-seo-services">'.ucwords($varcity).'</a></div>
Run Code Online (Sandbox Code Playgroud)

  • @SteveMeisner,实际上不是。他想去掉“破折号”,用空格代替它们。这就是函数的作用。 (2认同)

Sam*_*ook 43

<?php
echo '<div><a href="http://www.envisionforce.com/local/'.$row[website].'-seo-services">'.ucwords(str_replace("-"," ",$row[website])).'</a></div>';
Run Code Online (Sandbox Code Playgroud)

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

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

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


Shu*_*man 6

用空格代替破折号

str_replace("-"," ",$row[text])
Run Code Online (Sandbox Code Playgroud)

用破折号代替空间

str_replace(" ","-",$row[text])
Run Code Online (Sandbox Code Playgroud)


Nas*_*isi 5

str_replace('查找您要替换的内容'、'替换为'、'您的数组或字符串变量');

如果你想用空格替换破折号,你可以使用这个:

str_replace("-"," ",$row[text])
Run Code Online (Sandbox Code Playgroud)

如果要用破折号替换空格,请使用以下命令:

str_replace(" ","-",$row[text])
Run Code Online (Sandbox Code Playgroud)

使用 ucwords() 将单词大写。