I have a query pulling a bunch of city names. I need each of them to be linked… the link will look something like this:
http://www.sitename.com/city-new-york
Problem is, if I set the name to be the link of the names… some cities like “New York” has a space in them… and then the URL doesn’t work right.
How can I make it so that the spaces inside the name will be replaced with a “-” so that it will read “city-new-york”, right now I have this code but it links it as
“city-new york”
<?php foreach ($cities as $city):?>
<li>
<a href="city-<?php echo $city['name']; ?>">
<?php echo $city['name']; ?>
</li>
<?php endforeach;?>
Also, I just have to add… that I don’t have a choice, I HAVE to do it this way, because it’s part of an existing script… I can’t just add a separate column for URLs inside the db.
The
str_replacefunction allows you to replace characters in a string andstrtolowerwill make the whole thing lowercase:http://php.net/manual/en/function.str-replace.php
http://php.net/manual/en/function.strtolower.php