2012-11-17 5 views
5

मेरे पास तारों की सरणी है और मुझे सरणी में दिए गए स्ट्रिंग के आइटम के प्रत्येक मैच में हाइपरलिंक जोड़ना होगा। मुख्य रूप से विकिपीडिया में कुछ पसंद है।विकिपीडिया जैसे हाइपरलिंक्स के साथ प्रोग्रामेटिक रूप से HTML टेक्स्ट कैसे बनाएं?

कुछ की तरह:

private static string AddHyperlinkToEveryMatchOfEveryItemInArrayAndString(string p, string[] arrayofstrings) 

{   } 

string p = "The Domesday Book records the manor of Greenwich as held by Bishop Odo ofBayeux; his lands were seized by the crown in 1082. A royal palace, or hunting lodge, hasexisted here since before 1300, when Edward I is known to have made offerings at the chapel of the Virgin Mary.";

arrayofstrings = {"Domesday book" , "Odo of Bayeux" , "Edward"}; 

returned string = @"The <a href = "#domesday book">Domesday Book</a> records the manor ofGreenwich as held by Bishop <a href = "#odo of bayeux">Odo of Bayeux</a>; his lands wereseized by the crown in 1082. A royal palace, or hunting lodge, has existed here sincebefore 1300, when <a href = "#edward">Edward<a/> I is known to have made offerings at thechapel of the Virgin Mary.";

कि ऐसा करने का सबसे अच्छा तरीका क्या है?

+1

विकिपीडिया स्वचालित रूप से ऐसा नहीं करता है। आपको यह निर्दिष्ट करना होगा कि लिंक कहां हैं, भले ही आप पूर्ण HTML के बजाय एक सरल, विशिष्ट मार्कअप कर सकें। – GolezTrol

उत्तर

5

आप शायद काफी आसानी से इसे इस तरह कर सकता है:

foreach(string page in arrayofstrings) 
{ 
    p = Regex.Replace(
     p, 
     page, 
     "<a href = \"#" + page.ToLower() + "\">$0</a>", 
     RegexOptions.IgnoreCase 
    ); 
} 
return p; 

लंगर का पूंजीकरण मिलान वाला पाठ के रूप में ही किया जा सकता है, तो आप भी के लिए लूप से छुटकारा पाने के कर सकते हैं:

return Regex.Replace(
    p, 
    String.Join("|", arrayofstrings), 
    "<a href = \"#$0\">$0</a>", 
    RegexOptions.IgnoreCase 
); 

अब पैटर्न बन जाता है, मिलान पूंजीकरण के बावजूद पाए जाते हैं, और जो कुछ भी मिलता है उसे लिंक टेक्स्ट में और href विशेषता में वापस रखा जाता है।

संबंधित मुद्दे

 संबंधित मुद्दे