[thelist] splitting mysql result up into seperate words and making each a link

Steve Webster steve at netbreed.co.uk
Fri Sep 13 19:42:00 CDT 2002


Hiya,

You've basically got two choices. You can either explode the string into an
array and run them through a loop...


$keywords = "folding, bicycle, dahon";

// Split keywords into array
$keywords = explode(', ', $keywords);

// Output link for each keyword
foreach ($keywords as $keyword) {
    print "<a href=\"search.php?query=$keyword\">";
    print $keyword;
    print '</a>, ';
}


...or use a regular expression to wrap each keyword in a link...


$keywords = "folding, bicycle, dahon";

// Setup RegExp
$pattern = "([^ ,]*)";
$replace = "<a href=\"seach.php?query=\\1\">\\1</a>";

// Go RegExp
$keywords = ereg_replace($pattern, $replace, $keywords);

// Output links
print $keywords;


My tests seem to indicate that the latter is a little faster, but as I've
only got a list of 3 keywords the difference is negligable!

http://www.codejunkie.co.uk/test/keywords.php

HTH

Regards,

Steve

================================
 Steve Webster
 Freelance Web Developer
 steve at codejunkie.co.uk

 PHP, MySQL, Flash, Perl + more
 http://www.codejunkie.co.uk
================================

----- Original Message -----
From: "Dunstan Orchard" <dunstan at 1976design.com>
To: "evolt list" <thelist at lists.evolt.org>
Sent: Saturday, September 14, 2002 12:51 AM
Subject: [thelist] splitting mysql result up into seperate words and making
each a link


> Hi there,
>
> more php/mysql stuff I'm afraid.
>
>
>
> One of my db fields is a list of keywords related to an image, for
example:
>
> car, traffic, street, paper, motion, movement, america, transport
>
>
>
> I'm currently printing this out using this method:
>
> <?
> ..gubbins...
> $keywords = mysql_result($result, $i,"keywords");
> ..gubbins...
> ?>
>
> Keywords: <? print "$keywords"; ?>
>
> You can see that example here:
> http://www.1976design.com/testarea/photo/detail.php?number=4
>
>
> What I'd like to do is have _each_ of those words turned into a link which
> people can then click on to perform a search with that as the search term.
>
> Would anyone be kind enough to tell me how I go about splitting those
words
> up, popping links around them and printing them out again seperated by
commas.
>
>
>
> Thanks for all the continuing help, I've searched phpbuilder but don't
really
> know how to describe this task.
>
> cheers - dunstan
>
> ---------------------------
> Dorset, England
> http://www.1976design.com/
> http://www.orchard.it/
> http://www.maccaws.org/
> --
> For unsubscribe and other options, including
> the Tip Harvester and archive of thelist go to:
> http://lists.evolt.org Workers of the Web, evolt !
>
>





More information about the thelist mailing list