進一步測試所提供的解決方案,以解決我的問題,即在html標籤內部文字包裝文本並保留原始封閉標籤,例如<p>
或<b>
甚至<i>
標籤圍繞在設置位置切割的單詞。這對於某些需要特定格式以適用於前端的應用程序非常有用。我修改了提供的代碼以嘗試實現此目的,但是我一直沒有成功地使用這個php示例中使用的具有挑戰性的內容。如何在html標籤裏面保留標籤,同時保持標籤兩邊的標籤
我真的可以使用一些幫助,我確信其他人可以使用這些信息來構建動態的基於html的書籍,例如我正在嘗試的。它不限於書籍,也有滑塊和其他實現的可能性。通過不保留每個拆分周圍的結束標籤,您僅限於拆分不破壞周圍代碼的代碼,如<br>
標籤。我需要按順序拆分</div><div>
以關閉周圍的標籤,並將其重新打開,以供其他javascript片段使用的每個頁面以翻頁方式呈現每個頁面。
這是代碼我到目前爲止連同樣本數據:
<?php
function htmlWrapThing($str, $size, $breaking){
\t $html = false;
\t $i = 0;
\t $t = 0;
$tagcount = 0;
\t $chars = str_split($str);
\t $return = "";
\t $break = false;
\t $tag = "";
$newtag="";
\t foreach($chars as $char){
\t \t if($char=="<"){
$tagcount++;
$html = true;
}
\t \t if($char==">") $html = false;
\t \t
if(($tagcount/2)>1){
\t \t $tagcount = 0;
\t \t $tag = "";
\t \t }
\t \t if($html) $t++;
\t \t if($html && $char!="/" && $t > 1){
\t \t $tag .= $char;
\t \t $t =0;
\t \t }
\t \t
\t \t if(!$html) $i++;
\t \t $return .= $char;
\t \t if($i==$size) $break = true;
\t \t if($char == " " && !$html && $break){
\t \t \t
\t \t \t
if(!isset($tag)||$tag==""){
\t \t $return .= $breaking;
}else{
$return .= '</'.$tag.'>'.$breaking.'<'.$tag.'>';
}
\t \t \t \t
\t \t \t \t
\t \t \t $i=0;
\t \t \t
\t \t \t
\t \t \t $break = false;
\t \t }
\t \t \t
\t }
\t return $return;
}
$str = "<h1>hilo everybody how is everyone doing tonight?</h1><p>hello world how is everyone doing today</p><p>hello world how is everyone doing today</p><p>hello world how is everyone doing today</p><br><br />hello everybody how are you doing today?";
echo '<div class="pagecontent">'.htmlWrapThing($str,10, '</div><div class="pagecontent">').'</div>';
這生成輸出這樣的:
<div class="pagecontent"><h1>hilo everybody </h></div><div class="pagecontent"><h>how is everyone </h></div><div class="pagecontent"><h>doing tonight?</h1><p>hello </<></div><div class="pagecontent"><<>world how </<></div><div class="pagecontent"><<>is everyone </<></div><div class="pagecontent"><<>doing today</p><p>hello </<<pp></div><div class="pagecontent"><<<pp>world how </<<pp></div><div class="pagecontent"><<<pp>is everyone </<<pp></div><div class="pagecontent"><<<pp>doing today</p><p>hello </pp></div><div class="pagecontent"><pp>world how </pp></div><div class="pagecontent"><pp>is everyone </pp></div><div class="pagecontent"><pp>doing today</p><br><br />hello </b<r></div><div class="pagecontent"><b<r>everybody </b<r></div><div class="pagecontent"><b<r>how are you </b<r></div><div class="pagecontent"><b<r>doing today?</div>
當我需要的是更多像這樣的:
<div class="pagecontent"><h1>hilo everybody </h1></div>
<div class="pagecontent"><h1>1how is everyone </h1></div>
<div class="pagecontent"><h1>doing tonight?</h1><p>hello </p></div>
<div class="pagecontent"><p>world how </p></div>
<div class="pagecontent"><p>is everyone </p></div>
<div class="pagecontent"><p>doing today</p><p>hello </p></div>
<div class="pagecontent"><p>world how </p></div>
<div class="pagecontent"><p>is everyone </p></div>
<div class="pagecontent"><p>doing today</p><p>hello </p></div>
<div class="pagecontent"><p>world how </p></div>
<div class="pagecontent"><p>is everyone </p></div>
<div class="pagecontent"><p>doing today</p><br><br />hello</div><div class="pagecontent">everybody </div>
<div class="pagecontent">how are you</div>
<div class="pagecontent">doing today?</div>
正如你可以看到它是獲得重新插入標籤全亂了。任何想法如何解決這一問題?
雖然這並不制衡H1標籤這部作品令人難以置信的好,你說這麼快我希望我可以給你一百萬擊掌,因爲這工作這麼好! – 2014-10-31 02:00:58
@ BlaineA.Miller我不明白你的意思是平衡h1標籤。如果你可以試着解釋一下,也許我可以這樣做。 – 2014-10-31 02:06:28
基本上,h1標籤是在這個函數每次運行的開始,而css使這些顯示更大,我需要調整該頁面的大小和爲了讓它正確顯示,它打破了。例如,如果html標籤是h1,那麼通過減少200個字符來修改當前分頁符的大小,然後在每個分隔符之後留下其餘的頁面。 – 2014-10-31 02:10:25