如何将html标记的内部进行单词包装,同时将标记保持在包装的两侧


How do I word-wrap the inside of html tags while keeping the tags around both sides of the wraps

在进一步测试为我的问题提供的解决方案时,该解决方案是将文本换行到html标记中,并在设置位置剪切的单词周围保留原始的封闭标记,如<p><b>,甚至<i>标记。这对于某些需要特定格式才能为前端工作的应用程序非常有用。我已经修改了提供的代码来尝试实现这一点,但我在这个php示例中使用的具有挑战性的内容上没有成功。

我真的需要一些帮助,我相信其他人也可以像我试图做的那样,利用这些信息来构建基于html的动态书籍。它不仅限于书籍,还有滑块和其他实现的可能性。通过不在每个分割周围保留结束标记,您只能分割不会破坏周围代码的代码,例如<br>标记。我需要使用</div><div>进行拆分,以便关闭周围的标记,并为另一个javascript片段使用的每个页面重新打开它,以翻转书的方式呈现每个页面。

这是我迄今为止拥有的代码以及样本数据:

<?php
function htmlWrapThing($str, $size, $breaking){
	$html = false;
	$i = 0;
	$t = 0;
$tagcount = 0;
	$chars = str_split($str);
	$return = "";
	$break = false;
	$tag = "";
$newtag="";
	foreach($chars as $char){
		if($char=="<"){ 
$tagcount++;
$html = true;
}
		if($char==">") $html = false;
		
if(($tagcount/2)>1){
		$tagcount =  0;
		$tag = "";
		}
		if($html) $t++;
		if($html && $char!="/" && $t > 1){
		$tag .= $char;
		$t =0;
		}
		
		if(!$html) $i++;
		$return .= $char;
		if($i==$size) $break = true;
		if($char == " " && !$html && $break){
			
			
if(!isset($tag)||$tag==""){
		$return .= $breaking;
}else{
$return .= '</'.$tag.'>'.$breaking.'<'.$tag.'>';
}
				
				
			$i=0;
			
			
			$break = false;
		}
			
	}
	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>

正如你所看到的,它把重新插入的标签弄得一团糟。有什么办法解决这个问题吗?

已更新,因此不会在单词中间中断。

我只是把这个拼凑在一起。似乎可以很好地使用您的测试字符串。

这是一个PHP小提琴类型的东西

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; 
}