Jump to content

array?


ki

Recommended Posts

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

Link to comment
Share on other sites

<?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.

 

Link to comment
Share on other sites

<?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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

they way im talking about it is like this:

[linkforsearch]blah[/endlink], [linkforsearch]blah[/endlink], [linkforsearch]blah[/endlink], [linkforsearch]blah[/endlink]

like that

Link to comment
Share on other sites

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 />';
}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

[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]

Link to comment
Share on other sites

<?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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.