Php 用连字符替换空格?

Php 用连字符替换空格?,php,replace,substr,Php,Replace,Substr,我有这个 echo "<div class='weather " . $cell->textContent ."'>"; echo”“; 当textContent只是一个像“sunny”这样的词时,这种方法很好用。但是,如果当前天气是“部分多云”,我当然会申请两个课程(.partially and.cloudy)而不是.partially cloudy 如何确保在textContent中包含两个(或更多)单词时,我用连字符替换所有空格 这不起作用: echo "<di

我有这个

echo "<div class='weather " . $cell->textContent ."'>";
echo”“;
当textContent只是一个像“sunny”这样的词时,这种方法很好用。但是,如果当前天气是“部分多云”,我当然会申请两个课程(.partially and.cloudy)而不是.partially cloudy

如何确保在textContent中包含两个(或更多)单词时,我用连字符替换所有空格

这不起作用:

echo "<div class='weather " . substr_replace(" ", "-", $cell->textContent) ."'>";
echo”“;
谢谢。

使用:

echo”“;
使用:

echo”“;
echo "<div class='weather " . str_replace(" ", "-", $cell->textContent) ."'>";