Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Question (http://answers.yahoo.com/question/index?qid=20110704124838AADcKlm):
- I am making a website (using html css and php) in which a person can enter many tags into a single string. Then I need to make each tag into a click-able link. Does anyone know how to do that?
- User enters tags in a form => tags are saved into $tags => each individual tag is made into a click-able link
- soda coke bottle new => $tags="soda coke bottle new" => <a href="">soda</a>...
- */
- $tagDelimiter = ';'; // The character used to seperate tags. eg: ; or , or [space]. Keep in mind that using a space will not allow for two word tags, like New York or Los Angeles or etc.
- $linkTemplate = '<a href="find_tags.php?tag=%s">%s</a><br />';
- if($_SERVER['REQUEST_METHOD'] === 'POST'){ // If the form has POSTed to the page
- $tags = explode($tagDelimiter,trim($_POST['tags'],$tagDelimiter . ' ')); // Take the tags input and separate them into an array, using the delimiter as the marker to split the line.
- $tags = array_map('trim',$tags); // Cycle through each tag, cleaning the whitespace from either end of the tag.
- $c = count($tags); // Count the number of tags and store it in the variable $c
- $output = '';
- for($i=0;$i<$c;++$i){ // Cycle through the tags and create an $output var containing the HTML.
- $output .= sprintf($linkTemplate, urlencode($tags[$i]), $tags[$i]);
- }
- echo $output;
- }else{
- echo '
- <h3>Please use a \'' . $tagDelimiter . '\' to separate the tags</h3>
- <form method="POST">
- <input type="text" name="tags" /><br />
- <input type="submit" value="Format Tags" />
- </form>
- ';
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment