I've been trying to split a PHP string in an arbitrary number of characters per split. However, I'm looking for a way to do so without breaking HTML tags. Here is an example:
我一直试图在每次拆分时以任意数量的字符拆分PHP字符串。但是,我正在寻找一种不破坏HTML标签的方法。这是一个例子:
$string = 'Section 1:
<table width = "528" border="0" cellpadding="0" cellspacing="0">
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 1 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 2 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 3 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top">• </td> <td valign="top"> Element 4 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 5 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 6 </td></tr>
</table>
Section 2:
<table width = "528" border="0" cellpadding="0" cellspacing="0">
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 7 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 8 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 9 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 10 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 11 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 12 </td></tr>
<tr> <td width="20"> </td> <td width="15" valign="top"> • </td> <td valign="top"> Element 13 </td></tr>
</table>';
$charAmount = 450;
$textSplit = array();
while ($string){
array_push($textSplit, substr($string, 0, $charAmount));
$string = substr($string, $charAmount);
}
var_dump($textSplit);
In this case, two tags are broken. I'd like whatever tag that is cut up at the end of a split to just skip to the next split, but I have no idea how to do this.
在这种情况下,两个标签被打破。我喜欢在分割结束时切换的任何标签只是跳到下一个分割,但我不知道如何做到这一点。
2 个解决方案
#1
1
I'm not php guys, But logicwise I can help, just before split check which of dese two character is present nearest backwards from the split index <
or >
我不是php的人,但逻辑上我可以提供帮助,就在拆分之前检查哪个dese两个字符最接近拆分索引 <或>
if <
is encountered u r splitting in wrong place so skip
如果 <遇到你在错误的地方分裂,那么跳过< p>
if >
is encountered go ahead with split
如果>遇到>继续拆分
I have done it in jQuery successfully sometimes back
我有时成功地在jQuery中完成了它
#2
0
About Splitting html string, I have no ideas now but cutting string with limit character you could refer the solution at the link: https://github.com/dhngoc/php-cut-html-string. This resource may help you to get more ideas.
关于拆分html字符串,我现在没有任何想法,但是切割带限制字符的字符串你可以在链接中引用解决方案:https://github.com/dhngoc/php-cut-html-string。此资源可以帮助您获得更多想法。
#1
1
I'm not php guys, But logicwise I can help, just before split check which of dese two character is present nearest backwards from the split index <
or >
我不是php的人,但逻辑上我可以提供帮助,就在拆分之前检查哪个dese两个字符最接近拆分索引 <或>
if <
is encountered u r splitting in wrong place so skip
如果 <遇到你在错误的地方分裂,那么跳过< p>
if >
is encountered go ahead with split
如果>遇到>继续拆分
I have done it in jQuery successfully sometimes back
我有时成功地在jQuery中完成了它
#2
0
About Splitting html string, I have no ideas now but cutting string with limit character you could refer the solution at the link: https://github.com/dhngoc/php-cut-html-string. This resource may help you to get more ideas.
关于拆分html字符串,我现在没有任何想法,但是切割带限制字符的字符串你可以在链接中引用解决方案:https://github.com/dhngoc/php-cut-html-string。此资源可以帮助您获得更多想法。