ki Posted May 10, 2007 Share Posted May 10, 2007 okay how do i split say, i have this: blah,blah, blah,blah,blah, blah, blah, blah spaces and non spaces included, what im asking is how would i turn this and make each one of those a link to search the item in between the , and then link it to like page.php?search=blah, kinda like facebook does it, i know the search part but the array part is a blank to me, any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/ Share on other sites More sharing options...
per1os Posted May 10, 2007 Share Posted May 10, 2007 <?php $search = "some,string,word,seperated,by,a,comma"; $search = explode(",", $search); foreach ($search as $term) { echo '<a href="page.php?search=' . $term . '>Search for ' . $term . '</a><br />'; } ?> www.php.net/explode edit had explode function params backwards, it is now correct. Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-249976 Share on other sites More sharing options...
bbaker Posted May 10, 2007 Share Posted May 10, 2007 <?php $search = "some,string,word,seperated,by,a,comma"; $search = explode(",", $search); foreach ($search as $term) { echo '<a href="page.php?search=' . $term . '>Search for ' . $term . '</a><br />'; } ?> There's a missing " between ' and >Search should be ' . $term . ' " >Search Also...if you want those spaces removed, tray adding trim() to the $term variable in the link quote trim($term) Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-249994 Share on other sites More sharing options...
ki Posted May 10, 2007 Author Share Posted May 10, 2007 will that return the , too? Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250214 Share on other sites More sharing options...
utexas_pjm Posted May 11, 2007 Share Posted May 11, 2007 will that return the , too? No. The comma serves as the delimiter and will not exist once the string has been converted into an array. Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250250 Share on other sites More sharing options...
ki Posted May 11, 2007 Author Share Posted May 11, 2007 what if i made split it to get the first one then use the 2nd half to return foreach with a comma in front? Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250764 Share on other sites More sharing options...
Psycho Posted May 11, 2007 Share Posted May 11, 2007 what if i made split it to get the first one then use the 2nd half to return foreach with a comma in front? What? Instead of giving some generic example such as "blah,blah,blah" why don't you give a realistic example of the data you expect and how you want to deal with it. You could simply convert the string to an array and "shift" the first element off the array. Then you would ahve the first element in a variable and the rest in an array. Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250769 Share on other sites More sharing options...
ki Posted May 11, 2007 Author Share Posted May 11, 2007 they way im talking about it is like this: [linkforsearch]blah[/endlink], [linkforsearch]blah[/endlink], [linkforsearch]blah[/endlink], [linkforsearch]blah[/endlink] like that Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250801 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 So you would just change the eliminator in the explode function to ", " instead of ",". Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250803 Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250804 Share on other sites More sharing options...
Psycho Posted May 11, 2007 Share Posted May 11, 2007 That was already addressed before in this thread. You should just use the comma as a separator and then use trim on the resulting value. <?php //Convert string to array $searchStr = "some,string,word,seperated,by,a,comma"; $searchAry = explode(",", $searchStr); //Clean the values in the array using trim foreach ($searchAry as $term) { echo '<a href="page.php?search=' . trim($term) . '">Search for ' . trim($term) . '</a><br />'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250808 Share on other sites More sharing options...
ki Posted May 11, 2007 Author Share Posted May 11, 2007 thats not exactly what im looking to do; on this site, when you input say.... blah blah, blah blah blah, blah,blah blah,blah in a textarea, then after its posted and saved the database when someone retrieves it, it looks like <a href="search.php?s=$blah">$blah</a> then commas to seperate Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250816 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 I'm completely lost here, any chance of a proper example instead of blah blah? Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250821 Share on other sites More sharing options...
ki Posted May 11, 2007 Author Share Posted May 11, 2007 okay say i want to list some interests like: php, css,html, vb6 programming then return those into links and make them proper making it turn into <a href="search.php?s=$TERM">$TERM</a> then adding a comma at the end, but making it turn out like this: php, css, html, vb6 programming Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250826 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 [code]<?php $string = "php, css,html, vb6 programming"; $pieces = explode(",", $string); foreach ($pieces as $piece) { $piece = trim($piece); echo "<a href=\"search.php?s=$piece\">$piece</a><br />"; } ?> There [/code] Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250837 Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 <?php $string = 'php, css,html, vb6 programming'; $string = str_replace(', ', ',', $string); $string = str_replace(',', ', ', $string); echo '<a href="search.php?s=' . $string . '">' . $string . '</a>'; ?> Like that? Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250839 Share on other sites More sharing options...
ki Posted May 11, 2007 Author Share Posted May 11, 2007 close, but it didnt seperate the individual terms Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250843 Share on other sites More sharing options...
chigley Posted May 11, 2007 Share Posted May 11, 2007 Is my version any use ki? Not sure if you've seen it or not Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250845 Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 Well as far as I am concerned you have more than enough information in this topic alone to do what you want. Take bits and peices and manipulate it to what you want. Now it is getting ridiculous. Sometimes coding on your own is a good thing. Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250848 Share on other sites More sharing options...
Psycho Posted May 11, 2007 Share Posted May 11, 2007 I believe he wants the values in a single string to store int he database. Also you will need to urlencode the values for the links: <?php //Convert string to array $searchStr = "php, css,html, vb6 programming"; $searchAry = explode(",", $searchStr); //Change the array values to links foreach ($searchAry as $key => $term) { $searchAry[$key] = '<a href="page.php?search=' . rawurlencode(trim($term)) . '">' . trim($term) . "</a>"; } //Create a new string containing all the links comma separated $searchStr = implode(", ", $searchAry); echo $searchStr //OUTPUT: //<a href="page.php?search=php">php</a>, <a href="page.php?search=css">css</a>, //<a href="page.php?search=html">html</a>, <a href="page.php?search=vb6%20programming">vb6 programming</a> ?> EDIT: By the way, if you had taken a little time int he beginning to clearly explain the problem this post would not have gone to 2 pages. Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250854 Share on other sites More sharing options...
ki Posted May 11, 2007 Author Share Posted May 11, 2007 ah thank you this is exactly what i was looking for. and i did what im asking is how would i turn this and make each one of those a link to search the item in between the , and then link it to like page.php?search=blah, kinda like facebook does it, i know the search part but the array part is a blank to me, any help would be appreciated Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250866 Share on other sites More sharing options...
per1os Posted May 11, 2007 Share Posted May 11, 2007 Just be glad mjdamato has an extremely rare sense of making sense out of something that makes no sense. It would of been better stated like this: I am looking for something on how to take an array of search terms that are seperated by a , and making each search term into a link and than displaying it as it was originally presented IE: a termlink, othertermlink, finaltermlink format. Because what I gathered from that statement is you wanted to make an array of search terms, not that you wanted to make them display after turning them into a link the same way they originally were posted. How to ask the right question can be imparitive to getting the correct answer the first time =) Nice job on the decryption mjdamato! Quote Link to comment https://forums.phpfreaks.com/topic/50833-array/#findComment-250871 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.