maineyak Posted April 20, 2009 Share Posted April 20, 2009 I accidentally posted this in the wrong forum, I guess this is where I should have posted it. ----------------------------------------- Below is a code for a tag cloud, I would like to add a + to replace the spaces in the urls created, but cannot figure out how to do it. I'm no coder, but I can usually figure things out by googling and other methods, but this one has had me stumped for 2 days! So blue widget would have an url like http://www.domain.com/blue+widget rather than http://www.domain.com/blue widget I will be forever indebted if you can help me lol. <?php $CFG['db_host'] = 'localhost'; $CFG['db_user'] = 'USER'; $CFG['db_pass'] = 'PASSWORD'; $CFG['db_name'] = 'DB'; mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error()); mysql_select_db($CFG['db_name']); function tag_info() { $result = mysql_query("SELECT * FROM tags GROUP BY tag_name ORDER BY RAND() DESC LIMIT 40"); while($row = mysql_fetch_array($result)) { $arr[$row['tag_name']] = $row['count']; } //ksort($arr); return $arr; } function tag_cloud() { $min_size = 14; $max_size = 22; $tags = tag_info(); $minimum_count = min(array_values($tags)); $maximum_count = max(array_values($tags)); $spread = $maximum_count - $minimum_count; if($spread == 0) { $spread = 1; } $cloud_html = ''; $cloud_tags = array(); $step = ($max_size - $min_size)/($spread); foreach ($tags as $tag => $count) { $size = $min_size + ($count - $minimum_count) * $step; // $size = ($max_size + $min_size)/$spread; $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' . '" class="tag_cloud" href="/search-' . $tag .'-blended' . '" title="' . $count . ' searches for \'' . $tag . '\'">' . htmlspecialchars(stripslashes($tag)) . '</a>'; } $cloud_html = join("\n", $cloud_tags) . "\n"; return $cloud_html; } ?> <?php if (!eregi('[.@\"\'!/:%]', $_GET['k'])) if (mysql_affected_rows()){ mysql_query("UPDATE tags SET count=count+1 WHERE tag_name=\"".$_GET["k"]."\""); } if (!mysql_affected_rows()){ mysql_query("INSERT INTO tags SET tag_name=\"".$_GET["k"]."\", count=1, search_date=NOW()"); } ?> Link to comment https://forums.phpfreaks.com/topic/154943-need-help-with-cleaning-up-urls-in-a-tag-cloud/ Share on other sites More sharing options...
gaza165 Posted April 20, 2009 Share Posted April 20, 2009 well you can simply replace the space in your url with a + sign using str_replace <?php $url = "http://www.domain.com/blue widget"; $url = str_replace(" ","+",$url); echo $url; // http://www.domain.com/blue+widget ?> Link to comment https://forums.phpfreaks.com/topic/154943-need-help-with-cleaning-up-urls-in-a-tag-cloud/#findComment-815024 Share on other sites More sharing options...
alphanumetrix Posted April 20, 2009 Share Posted April 20, 2009 He's right, str_replace() will work, but seeing as you're "not a coder," I'm guessing you wouldn't know how to intergrate that. This *should* work. <?php $CFG['db_host'] = 'localhost'; $CFG['db_user'] = 'USER'; $CFG['db_pass'] = 'PASSWORD'; $CFG['db_name'] = 'DB'; mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error()); mysql_select_db($CFG['db_name']); function tag_info() { $result = mysql_query("SELECT * FROM tags GROUP BY tag_name ORDER BY RAND() DESC LIMIT 40"); while($row = mysql_fetch_array($result)) { $arr[$row['tag_name']] = $row['count']; } //ksort($arr); return $arr; } function tag_cloud() { $min_size = 14; $max_size = 22; $tags = tag_info(); $minimum_count = min(array_values($tags)); $maximum_count = max(array_values($tags)); $spread = $maximum_count - $minimum_count; if($spread == 0) { $spread = 1; } $cloud_html = ''; $cloud_tags = array(); $step = ($max_size - $min_size)/($spread); foreach ($tags as $tag => $count) { $size = $min_size + ($count - $minimum_count) * $step; // $size = ($max_size + $min_size)/$spread; $theurl = htmlspecialchars(stripslashes($tag)); $theurl = str_replace(" ", "+", $theurl); $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' . '" class="tag_cloud" href="/search-' . $tag .'-blended' . '" title="' . $count . ' searches for \'' . $tag . '\'">' . $theurl . '</a>'; } $cloud_html = join("\n", $cloud_tags) . "\n"; return $cloud_html; } ?> <?php if (!eregi('[.@\"\'!/:%]', $_GET['k'])) if (mysql_affected_rows()){ mysql_query("UPDATE tags SET count=count+1 WHERE tag_name=\"".$_GET["k"]."\""); } if (!mysql_affected_rows()){ mysql_query("INSERT INTO tags SET tag_name=\"".$_GET["k"]."\", count=1, search_date=NOW()"); } ?> Link to comment https://forums.phpfreaks.com/topic/154943-need-help-with-cleaning-up-urls-in-a-tag-cloud/#findComment-815030 Share on other sites More sharing options...
maineyak Posted April 21, 2009 Author Share Posted April 21, 2009 This returns a blank page, what would be causing that? He's right, str_replace() will work, but seeing as you're "not a coder," I'm guessing you wouldn't know how to intergrate that. This *should* work. <?php $CFG['db_host'] = 'localhost'; $CFG['db_user'] = 'USER'; $CFG['db_pass'] = 'PASSWORD'; $CFG['db_name'] = 'DB'; mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error()); mysql_select_db($CFG['db_name']); function tag_info() { $result = mysql_query("SELECT * FROM tags GROUP BY tag_name ORDER BY RAND() DESC LIMIT 40"); while($row = mysql_fetch_array($result)) { $arr[$row['tag_name']] = $row['count']; } //ksort($arr); return $arr; } function tag_cloud() { $min_size = 14; $max_size = 22; $tags = tag_info(); $minimum_count = min(array_values($tags)); $maximum_count = max(array_values($tags)); $spread = $maximum_count - $minimum_count; if($spread == 0) { $spread = 1; } $cloud_html = ''; $cloud_tags = array(); $step = ($max_size - $min_size)/($spread); foreach ($tags as $tag => $count) { $size = $min_size + ($count - $minimum_count) * $step; // $size = ($max_size + $min_size)/$spread; $theurl = htmlspecialchars(stripslashes($tag)); $theurl = str_replace(" ", "+", $theurl); $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' . '" class="tag_cloud" href="/search-' . $tag .'-blended' . '" title="' . $count . ' searches for \'' . $tag . '\'">' . $theurl . '</a>'; } $cloud_html = join("\n", $cloud_tags) . "\n"; return $cloud_html; } ?> <?php if (!eregi('[.@\"\'!/:%]', $_GET['k'])) if (mysql_affected_rows()){ mysql_query("UPDATE tags SET count=count+1 WHERE tag_name=\"".$_GET["k"]."\""); } if (!mysql_affected_rows()){ mysql_query("INSERT INTO tags SET tag_name=\"".$_GET["k"]."\", count=1, search_date=NOW()"); } ?> Link to comment https://forums.phpfreaks.com/topic/154943-need-help-with-cleaning-up-urls-in-a-tag-cloud/#findComment-815390 Share on other sites More sharing options...
gffg4574fghsDSGDGKJYM Posted April 21, 2009 Share Posted April 21, 2009 Use urlencode() OR rawurlencode() instead of str_replace() http://www.php.net/manual/en/function.urlencode.php http://www.php.net/manual/en/function.rawurlencode.php And use urldecode() or rawurldecode() on the others end. Link to comment https://forums.phpfreaks.com/topic/154943-need-help-with-cleaning-up-urls-in-a-tag-cloud/#findComment-815482 Share on other sites More sharing options...
maineyak Posted April 21, 2009 Author Share Posted April 21, 2009 I just played around with a bit more, and this is what worked: $tag = htmlspecialchars(stripslashes($tag)); $tags = htmlspecialchars(stripslashes($tag)); $tag = str_replace(" ", "+", $tag); $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' . '" class="tag_cloud" href="/search-' . rawurlencode($tag) .'-blended' . '" title="' . $count . ' searches for \'' . $tag . '\'">' . $tags . '</a>'; } Thanks for the help! Link to comment https://forums.phpfreaks.com/topic/154943-need-help-with-cleaning-up-urls-in-a-tag-cloud/#findComment-815692 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.