i read some tutorials but couldn't get it worked so i need help now. here is the code i use: <?php $conn = mysql_connect("localhost", ****, ****); mysql_select_db($DB); $sql = mysql_query("SELECT * FROM ***** ORDER BY rand() LIMIT 0,50"); while($result = mysql_fetch_array($sql)) { echo "<a href=/search/0/".$result["search_id"] . "-" .$result[search] . ".html>" .$result["search"] . "</a> "; } ?> PHP: this way it shows the search url with space in it, i want to replace it with "-" i know i must use str_replace but where and how, it confused me can any of you help me?
Instead of your while put this one: while($result = mysql_fetch_array($sql)) { $search = preg_replace('/\s/', '-', $result[search]); echo "<a href=/search/0/".$result["search_id"] . "-" .$search . ".html>" .$result["search"] . "</a> "; } PHP:
Don't use preg_replace unless you have a reason to, because it's slower than str_replace. To make it any useful, you could just add a plus sign after the \s to make it replace multiple spaces with just one dash.
well i hope that u urlencode everything that is left after you strip that url. Or maybe you should strip anything that is not letter or digit ? ( smth like: preg_replace('/[^a-zA-Z0-9\-]/', '-', $result[search]); PHP: Good luck.