2014-10-31 158 views
-1

进一步测试所提供的解决方案,以解决我的问题,即在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>

正如你可以看到它是获得重新插入标签全乱了。任何想法如何解决这一问题?

回答

0

已更新,所以它不会在一个词​​的中间断开。

我只是把它扔在一起。似乎与您的测试字符串正常工作。

这里有一个PHP-fiddle-type-thing

function htmlWrapThing($str, $size){ 
    $html = false; 
    $i = 0; 
    $chars = str_split($str); 
    $return = ""; 
    $break = false; 
    foreach($chars as $char){ 
     if($char=="<") $html = true; 
     if($char==">") $html = false; 
     if(!$html) $i++; $return .= $char; 
     if($i==$size) $break = true; 
     if($char == " " && !$html && $break){ 
      $return .= "<br>"; 
      $i=0; $break = false; 
     } 
    } 
    return $return; 
} 

只是为了好玩,这里有同样的功能,略作修改为JS使用。还有一个JSFiddle

function HtmlTrimThing($chars, $size){ 
    $html = false; 
    $break = false; 
    $i = 0; 
    $return = ""; 
    $c = $d = $chars.length; 
    while($c--){ 
     $char = $chars[$d-$c-1]; 
     if($char=="<") $html = true; 
     if($char==">") $html = false; 
     if(!$html) $i++; 
     $return += $char; 
     if($i==$size) $break = true; 
     if($break && $char == " " && !$html){ 
      $return += "<br>"; 
      $i=0; 
      $break = false; 
     } 
    } 
    return $return; 
} 
+0

虽然这并不制衡H1标签这部作品令人难以置信的好,你说这么快我希望我可以给你一百万击掌,因为这工作这么好! – 2014-10-31 02:00:58

+0

@ BlaineA.Miller我不明白你的意思是平衡h1标签。如果你可以试着解释一下,也许我可以这样做。 – 2014-10-31 02:06:28

+0

基本上,h1标签是在这个函数每次运行的开始,而css使这些显示更大,我需要调整该页面的大小和为了让它正确显示,它打破了。例如,如果html标签是h1,那么通过减少200个字符来修改当前分页符的大小,然后在每个分隔符之后留下其余的页面。 – 2014-10-31 02:10:25