[英]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:但我需要的是让它在没有破折号的情况下显示,以便 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.如果代码可以限制在这一行,那就太好了,因为我在页面中还有很多其他内容要做。
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>
<?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.在上面的示例中,您可以使用
str_replace()
用空格替换连字符以创建单独的单词。 Then use ucwords()
to capitalize the newly created words.然后使用
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
replace dash with space用空格替换破折号
str_replace("-"," ",$row[text])
replace space with dash用破折号替换空格
str_replace(" ","-",$row[text])
str_replace ('Find what you want to replace', 'Replace with ', 'Your array or string variable'); str_replace('查找您要替换的内容'、'替换为'、'您的数组或字符串变量');
If you want to replace dash with space, you can use this:如果你想用空格替换破折号,你可以使用这个:
str_replace("-"," ",$row[text])
If you want to replace space with dash, use this:如果要用破折号替换空格,请使用以下命令:
str_replace(" ","-",$row[text])
Use ucwords() to capitalize words.使用 ucwords() 将单词大写。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.