How can I assign links to words in a string?

In a table I have a column containing names that I want to assign URLs to.

http://kissbandstree.com/artists.php

The list:

Eric S. = <a href='/artist.php?a=eric_singer'>
Eric C. = <a href='/artist.php?a=eric_carr'>
Bruce = <a href='/artist.php?a=bruce_kulick'>
Ace = <a href='/artist.php?a=ace_frehley'>
Gene = <a href='/artist.php?a=gene_simmons'>
Paul = <a href='/artist.php?a=paul_stanley'>
Peter = <a href='/artist.php?a=peter_criss'>
Vinnie = <a href='/artist.php?a=vinnie_vincent'>
Mark = <a href='/artist.php?a=mark_st_john'>
Tommy = <a href='/artist.php?a=tommy_thayer'>

There can be several names per table cell, for instance:

Ace, Eric S., Bruce

The names are to be links, and the white spaces and commas between names logically not.

The output Iā€™m trying to achieve would be:

<a href='/artist.php?a=ace_frehley'>Ace</a>, <a href='/artist.php?a=eric_singer'>Eric S.</a>, <a href='/artist.php?a=bruce_kulick'>Bruce</a>

This is the code (without links) thatā€™s in use now:

echo "<td class='notwrap' style='text-align: left;' title='" . trim($contents[5]) . "'>" . trim($contents[5]) . "</td>";

so you use an array to store the names? why are we trimming the values? is this user input?
maybe you can show us a bit more code, so we can better understand what you are doing.

1 Like

Hi. Thanks for swift, kind reply.

Source code: http://kissbandstree.com/artists.txt

Iā€™ve thought about storing the names and link counterpart in an array.

The vaules are from a .txt file on the server. Iā€™m a flat file person.

This is the example file: http://kissbandstree.com/kiss_artists/karl_cochran.txt

This is not user input, no.

All the best - MC Peko

Hello, i see now what you are doing. Iā€™ve spent near two hours on this question but i have things to do now. What you want seems to be complicated because you need to maintain a comma and space between names and you also need to insert links which are not in the text file ( href=ā€™/artist.php?a=ace_frehleyā€™).

you could use explode to read the amount but this doesnā€™t solve the comma plus space issue. your table design doesnā€™t make room for a break tag either.

her is a way to read this but not format it:

    $isMultiLine = 0;
    if (count(explode(",", $contents[5])) > 1) {
        $showResult = explode(",", $contents[5]); ++$isMultiLine;
    }

    if (!$isMultiLine) {
        echo "<td class='notwrap' style='text-align: left;' title='" . $contents[5] . "'>" . $contents[5] . "</td>"; // played with
    } else {
        echo "<td class='notwrap' style='text-align: left;' title=''>";
        for ($i=0; $i < count($showResult); $i++) {
          echo "<a href=''>" . $showResult[$i] . "</a><br />";
        }
        echo "</td>";
    }

Are you reusing the data in the text files? if you are just reading the data from the text file without a need to read specific parts in a different instance, then why not hard code the results? it would be easier for you:

<td class='notwrap' style='text-align: left;' title='Karl Cochran'><a href='/artist.php?a=karl_cochran'>Karl Cochran</a></td>
<td class='notwrap' style='text-align: left;' title='bass, vocals'>bass, vocals</td>
<td class='notwrap' style='text-align: left;' title='https://facebook.com/karl.cochran.9'><a href='https://facebook.com/karl.cochran.9'>karl.cochran.9</a></td>
<td class='notwrap' style='text-align: left;' title='male'>male</td>
<td class='notwrap' style='text-align: left;' title='alive'>alive</td>
<td class='notwrap' style='text-align: left;'><a href='/artist.php?a=ace_frehley'>Ace</a>, <a href='/artist.php?a=eric_singer'>Eric S.</a>, <a href='/artist.php?a=bruce_kulick'>Bruce</a></td>
<td class='notwrap' style='text-align: left;'>&nbsp;</td>
<td class='notwrap' style='text-align: left;' title='http://karlcochran.com'><a href='http://karlcochran.com'>karlcochran.com</a></td>
00-00-0000
1 Like

You would use explode() to break the list of name(s) into an array, produce a link from each name, and use implode() to put the links into a list.

<?php

// define a mapping of name to file
$map = [];
$map['Eric S.'] = 'eric_singer';
// add other mappings here...

// at the point of processing the list of names...
// explode the list of names - works for one or more names
$list = explode(',',$contents[5]);
// trim all the values at once
$list = array_map('trim',$list);

// produce the link(s)
$links = [];
foreach($list as $name)
{
	// lookup the file that corresponds to the name
	$file = $map[$name];
	// if there might not be a file for a name, add conditional logic here to handle that case
	$links[] = "<a href='/artist.php?a=$file'>$name</a>";
}

// output the result - works for one or more names
echo implode(', ',$links);

The $map array should only contain the values, not the whole link, so that it is general purpose and can be reused for different purposes and so you are not repeating code/markup. If you choose to change something about the links, the method shown here only has one existence of the markup, and only one place it needs to be changed.

1 Like

Because a point we have been trying to make is to use the computer as a tool to accomplish work, rather than spending your time beating on a keyboard writing repetitive hard coded, error prone, hard to maintain markup like it is the 1990ā€™s.

Hello phdr and Happy Anniversary. :slight_smile:

honestly, i already know that in order to accomplish this task: we need to make an array, explode and make an array map for links. the opposite of explode is implode. Iā€™m not stupid. I lack experience to be able to slap together code in minutes. I would need a week to code this. I am a newbie. I got as far as the array map and the list with exploded info.

i do, however, take exception to insulting code that you disagree with as outdated. I like to utilize programming too but there is also a limit to this idea: arrays use memory. memory is costly compared to hard disks. the tiny text file stored on disk is a cakewalk compared to four arrays in memory multiplied by the amount of the users.

I feel like you would insult my website design but but i donā€™t have to pay someone thousands of dollars for it or attend a universirty for four years to start making it. Iā€™ve seen alot worse material online and they make alot of money. Have you seen Amazon? millionaires and this is the best web design you can come up with? blocks and all white and buy now, pay now, recommended for you? and scroll, scroll, scroll to hell and back like Sony. Framework cookie cutters, no doubt. I could write a book about how much frustration these sites cause with db connections, arrays, ā€˜dynamicā€™ data and analytics slowing down my browsing experience. Sometimes retro is better. Iā€™d rather play DK64 than Halo.

1 Like

Hi johnphpnewb. And thank you so much for you great work here. :slight_smile:

You interpret the situation 100% correct.

And as weā€™re dealing with only ten words (people) here, leaning towards hard-code, or at least ā€œhard-codeā€ is not a bad idea, imo. An array with these ten words and their respective URLs is hard-code in the strict meaning of the expression.

Iā€™ll read through everything you wrote now, and try out your great suggestions.

Hi phdr.

Thank you so much. This looks awesome. Trying it out now.

I added

>"

to

$links[] = "<a href='/artist.php?a=$file'>$name</a;

so itā€™s now

$links[] = ā€œ$nameā€;

The code works like a charm phdr! :slight_smile:

Implemented: http://kissbandstree.com/artists.php

Code: http://kissbandstree.com/artists.txt

I even understand most of the code, lols. Could never have some up with it myself.

I might have a couple of small follow up questions or comments. Seeing this live in action, the upgrade improved the site even more than I had imagined.

Thank you guys so much again! Bless.

Announcement and thanks: https://facebook.com/kissbandstree/photos/a.804756169658639/1504159653051617. :slight_smile:

1 Like

Implemented also on individual artist pages now. :slight_smile:

For instance
http://kissbandstree.com/artist.php?a=tod_howarth
now links to
http://kissbandstree.com/artist.php?a=ace_frehley
.

1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service