I have a few strings
Computer Stations
Monitors
Indian Reserve
How do i replace spaces in a variable with carriage returns "\r"? How can I determine if a replacement was made so I can add a CSS class in my output.
My PHP is below and basically for CSS purposes i need to force a carriage return on two words and if the string is a single word then give the class “single” so i can style it differently…any ideas
$key "Computer Stations"
echo "<li><a href=\"#\" class=\"cat cat-btn lf\">{$key}</a></li>";
$key "Monitors"
echo "<li><a href=\"#\" class=\"cat cat-btn single lf\">{$key}</a></li>";
$key "Indian Reserve"
echo "<li><a href=\"#\" class=\"cat cat-btn lf\">{$key}</a></li>";
Following code will replace all spaces with return symbol (
"\r") and if there isn’t any, set variable$classto"single".By return you mean newline? If so, you should use
"\n"instead of"\r". If you would like to see the newline in a browser not just in the code you should use'<br />'instead.