Click here to Skip to main content
15,879,239 members
Articles / Programming Languages / C#
Tip/Trick

Convert a list of names to HTML Wikipedia links

Rate me:
Please Sign up or sign in to vote.
5.00/5 (4 votes)
8 Aug 2014CPOL3 min read 26.8K   5   11
How to convert a list of names to the HTML needed to create a link to the corresponding Wikipedia articles

The Eagles Have Landed

If you have a list of names of famous people such as this:

  • Glenn Frey
  • Don Henley
  • Bernie Leadon
  • Randy Meisner
  • Joe Walsh
  • Timothy B. Schmit
  • Don Felder
  • Jackon Browne
  • J.D. Souther

...you may want to linkify the names to point to their respective wikipedia articles. To do so is not difficult, because wikipedia follows a standard format for the URLs for its entries - in most cases for proper names, it simply replaces the space between the first and last name with an underscore, like so: "Chris Hillman" becomes "Chris_Hillman". The link for the Byrdman of Bakersfield (poetic license) is: href="http://en.wikipedia.org/wiki/Chris_Hillman"

There's always an exception, though (there may be an exception to that rule, too, which would be nice). In this case, though, there's not an exception to the exception rule (oh, pooh!), so we have to watch out for "special cases" such as names with initials. In this case, wikipedia retains the period but places an underscore after it, such as: http://en.wikipedia.org/wiki/A._A._Milne

Still, programatically transforming names into the accepted format is pretty easy. Here's some example code; you can pass the name of any person with a wikipedia page to the Wikipidify() method to get back the appropriate HTML for the link to that wikipedia page.

The sample code uses names of current and former members of The Eagles (the Eagles from Southern California, not the Philadelphia Eagles, football fans), along with the names of some affiliated/associated people, such as "The Fifth Eagle," Jackson Browne, as well as songwriter J.D. Souther. A fictional character, Rupert Pupkin (The King of Comedy ) is also included to show what happens when a person in the list does not have a wikipedia page (the link is created fine, but clicking it takes you to a "Winchester Mystery page").

C#
private void buttonYourLip_Click(object sender, EventArgs args)
{
    List<string> eaglets = new List<string>();
    eaglets.Add("Glenn Frey");
    eaglets.Add("Don Henley");
    eaglets.Add("Bernie Leadon");
    eaglets.Add("Randy Meisner");
    eaglets.Add("Joe Walsh");
    eaglets.Add("Timothy B. Schmit");
    eaglets.Add("Don Felder");
    eaglets.Add("Jackon Browne");
    eaglets.Add("J.D. Souther");
    eaglets.Add("Rupert Pupkin");

    List<string> htmlifiedEaglets = new List<string>();

    foreach (var eaglet in eaglets)
    {
        htmlifiedEaglets.Add(Wikipidify(eaglet));
    }
    System.IO.File.WriteAllLines(@"C:\alreadyGone\eaglets.txt", htmlifiedEaglets);
}

private string Wikipidify(string eaglet)
{
    const String wikipediaTemplate = "<a href=\"http://en.wikipedia.org/wiki/{0}\"  target=\"_blank\">{1}</a>";
    string HTMLifiedEaglet = String.Empty;
    string underlinedEaglet = String.Empty;
    // make sure each "." is followed by a space
    underlinedEaglet = eaglet.Replace(".", ". ");
    // ...if there are now two spaces anywhere (such as where a period was already followed by a space), make them one 
    underlinedEaglet = underlinedEaglet.Replace("  ", " "); 
    // Now replace the single space[s] with underscore[s]
    underlinedEaglet = underlinedEaglet.Replace(" ", "_");
            
    HTMLifiedEaglet = String.Format(wikipediaTemplate, underlinedEaglet, eaglet);
    return HTMLifiedEaglet;
}
</string></string></string></string>

As you can see, the results are saved to a file. The file created for the list shown above looks like this:

<a href="http://en.wikipedia.org/wiki/Glenn_Frey" target="_blank">Glenn Frey</a> <a href="http://en.wikipedia.org/wiki/Don_Henley" target="_blank">Don Henley</a> <a href="http://en.wikipedia.org/wiki/Bernie_Leadon" target="_blank">Bernie Leadon</a> <a href="http://en.wikipedia.org/wiki/Randy_Meisner" target="_blank">Randy Meisner</a> <a href="http://en.wikipedia.org/wiki/Joe_Walsh" target="_blank">Joe Walsh</a> <a href="http://en.wikipedia.org/wiki/Timothy_B._Schmit" target="_blank">Timothy B. Schmit</a> <a href="http://en.wikipedia.org/wiki/Don_Felder" target="_blank">Don Felder</a> <a href="http://en.wikipedia.org/wiki/Jackson_Browne" target="_blank">Jackson Browne</a> <a href="http://en.wikipedia.org/wiki/J._D._Souther" target="_blank">J.D. Souther</a> <a href="http://en.wikipedia.org/wiki/Rupert_Pupkin" target="_blank">Rupert Pupkin</a>

If you don't want to take the time to add this code to a project just now, you can see the list of these generated links here

Outro

 

As you might surmise, I am a big fan of the Eagles; I bought their debut (eponymous) LP when it was still warm to the touch - in my birthplace of Fort Bragg, California. Although nobody asked me, I feel their best album - a masterpiece! - is the concept album "Desperado" (it also has the best back cover photo of all time).

Fade Out

 

I like almost everything the Eagles ever did, through all their personnel changes, with one blatant and glaring exception: "The Best of My Love" is one of the lamest songs I've ever heard, vying with the Commodore's "Three Times a Lady" for the most gag-worthy song of all time from otherwise good-great bands. If you agree with me that they sound like they've been hypnotized by Barry Manilow on that tune, go to your window, open it, lean out and shout at the tip-top of your voice: "I'm fed up, and I'm not gonna take it any more! No more plodding, saccharine love songs by people who can do so much better!!!"

Not Fade Away

 

You can purchase any of the Eagles albums from here

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
QuestionPeople WIth Identical Names Pin
PeejayAdams11-Aug-14 3:00
PeejayAdams11-Aug-14 3:00 
QuestionAnd what about other names? Pin
Pete O'Hanlon10-Aug-14 11:01
mvePete O'Hanlon10-Aug-14 11:01 
AnswerRe: And what about other names? Pin
B. Clay Shannon10-Aug-14 18:15
professionalB. Clay Shannon10-Aug-14 18:15 
GeneralMy vote of 5 Pin
Volynsky Alex8-Aug-14 21:52
professionalVolynsky Alex8-Aug-14 21:52 
SuggestionTypo? - You are replacing a space with a space! Pin
DaveAuld7-Aug-14 19:11
professionalDaveAuld7-Aug-14 19:11 
GeneralRe: Typo? - You are replacing a space with a space! Pin
B. Clay Shannon7-Aug-14 19:20
professionalB. Clay Shannon7-Aug-14 19:20 
GeneralRe: Typo? - You are replacing a space with a space! Pin
DaveAuld7-Aug-14 19:28
professionalDaveAuld7-Aug-14 19:28 
SuggestionAPI addition Pin
pritaeas6-Aug-14 21:35
professionalpritaeas6-Aug-14 21:35 
GeneralRe: API addition Pin
Florian Rappl9-Aug-14 0:20
professionalFlorian Rappl9-Aug-14 0:20 
GeneralRe: API addition Pin
B. Clay Shannon9-Aug-14 3:08
professionalB. Clay Shannon9-Aug-14 3:08 
GeneralRe: API addition Pin
.:floyd:.9-Aug-14 8:04
.:floyd:.9-Aug-14 8:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.