我正在嘗試在字符串內(nèi)的單詞之間添加 HTML 標(biāo)簽(用 html 標(biāo)簽包裝單詞,即 HTML 注釋)。HTML 標(biāo)記應(yīng)該寫入的位置由偏移數(shù)組分隔,例如://array(Start offset, End offset) in characters//Note that annotation starts in the Start offset number and ends before the End offset number$annotationCharactersPositions= array( 0=>array(0,3), 1=>array(2,6), 2=>array(8,10));因此,要使用以下 HTML 標(biāo)記 ($tag) 注釋以下 HTML 文本 ($source)。這是包裝由 $annotationPositions 數(shù)組分隔的字符(不考慮源的 HTML 標(biāo)記)。$source="<div>This is</div> only a test for stackoverflow";$tag="<span class='annotation n-$cont'>";結(jié)果應(yīng)如下(https://jsfiddle.net/cotg2pn1/):charPos =--------------------------------- 01---------------------------- 2-------------------------------------------3------------------------------------------45-------67-----------------------------89-------10,11,12,13......$output = "<div><span class='annotation n-1'>Th<span class='annotation n-2'>i</span></span><span class='annotation n-2'>s</span><span class='annotation n-2'> i</span>s</div> <span class='annotation n-3'>on</span>ly a test for stackoverflow"如何編寫下一個(gè)函數(shù): $cont=0; $myAnnotationClass="placesOfTheWorld"; for ($annotationCharactersPositions as $position) { $tag="<span class='annotation $myAnnotationClass'>"; $source=addHTMLtoString($source,$tag,$position); $cont++; }考慮到在計(jì)算 $annotationCharactersPositions 數(shù)組中描述的字符時(shí)不得考慮輸入字符串的 HTML 標(biāo)簽,并且必須考慮在 $source 文本中插入注釋(即$tag)的每個(gè)以下注釋的封裝/注釋。整個(gè)過(guò)程的想法是,給定輸入文本(可能包含也可能不包含 HTML 標(biāo)簽),將注釋一組字符(屬于一個(gè)或多個(gè)單詞),以便結(jié)果將具有選定的字符(通過(guò)數(shù)組它定義了每個(gè)注釋的開(kāi)始和結(jié)束位置)由 HTML 標(biāo)記包裝,該標(biāo)記可以通過(guò)可變數(shù)量的 html 屬性(名稱、類、id、數(shù)據(jù)-*)變化(a、span、mark)。此外,結(jié)果必須是格式良好的有效 HTML文檔,以便如果在多個(gè)注釋之間有任何注釋,則 html 應(yīng)相應(yīng)地寫入輸出。你知道任何圖書館或解決方案來(lái)做到這一點(diǎn)嗎?也許 PHP DOMDocument 功能可能有用??但是如何將偏移量應(yīng)用于 php DomDocument 函數(shù)?任何想法或幫助都受到歡迎。注 1:輸入文本是 UTF-8 原始文本,嵌入了任何類型的 HTML 實(shí)體 (0-n)。注 2:輸入標(biāo)簽可以是任何具有可變數(shù)量屬性 (0-n) 的 HTML 標(biāo)簽。注 3:初始位置必須是包含的,最終位置必須是不包含的。即 1o 注釋在第 2 個(gè)字符(包括第 2 個(gè)字符 'i')之前開(kāi)始并在第 6 個(gè)字符之前結(jié)束(不包括 6 個(gè)字符 's')
- 1 回答
- 0 關(guān)注
- 138 瀏覽
添加回答
舉報(bào)
0/150
提交
取消