开发者

php substr_replace space with hyphen?

开发者 https://www.devze.com 2023-02-15 02:48 出处:网络
i have this echo \"<div class=\'weather \" . $cell->textContent .\"\'>\"; this works fine when textContent is just one word like \"sunny\". However if the current weather is e.g. \"partial

i have this

echo "<div class='weather " . $cell->textContent ."'>";

this works fine when textContent is just one word like "sunny". However if the current weather is e.g. "partially cloudy" i get of course two classes applied (.partially and .cloudy instead of .partially-cloudy.

How can I make sure whenever there are two (or开发者_如何学C more) words contained in textContent I replace all spaces with a hyphen?

This does not work:

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

Thank you.


Use str_replace:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号