使用PHP将HTML一分为二

使用PHP将HTML一分为二,php,html,Php,Html,我试图将一串文本一分为二,注意不要: 破口大骂 中断html 给你一点背景,我想写一篇博客文章,并在中间插入一个广告。 我四处寻找答案,但我能找到的唯一选项是剥离所有HTML–这不是一个选项 例如: $string = "<div class='some-class'>Deasdadlights blasdasde holysadsdto <span>Bri<img src='#'>cable holystone blow the man down&l

我试图将一串文本一分为二,注意不要:

  • 破口大骂
  • 中断html
给你一点背景,我想写一篇博客文章,并在中间插入一个广告。

我四处寻找答案,但我能找到的唯一选项是剥离所有HTML–这不是一个选项

例如:

  $string = "<div class='some-class'>Deasdadlights blasdasde holysadsdto <span>Bri<img src='#'>cable holystone blow the man down</span></div>";
  $length = strlen($string);

  // At this point, something magical needs to split the string being mindful of word breaks and html
  $pieces = array(
    substr( $string, 0, ( $length / 2 ) ),
    substr( $string, ( $length / 2 ), $length )
  );

  echo $pieces[0] . " **Something** " . $pieces[1];

  // <div class="some-class">Deasdadlights blasdasde holysadsdto <spa **something**="" n="">Bri<img src="#">cable holystone blow the man down</spa></div>
  // And there goes the <span> tag :'(
$string=“Deasdadlights blasdasde Holysadto Bricable holystone将男子击倒”;
$length=strlen($string);
//在这一点上,需要一些神奇的东西来拆分字符串,注意分词和html
$pieces=数组(
substr($string,0,($length/2)),
substr($string,($length/2),$length)
);
echo$件[0]。“**某物**”$作品[1];
//死亡之灯将布拉斯达斯德·霍利萨德吹倒在砖石上
//标签上写着:'(
更新

感谢@Naga的回答!这里有一个稍微扩展的版本供需要它的人使用:

$string = '
  <section class="post_content">
    <p>Often half the battle is ensuring you get the time to respond to your reviews, the other half is remembering that the customer is always right and you should proceed with caution.</p>
    <p>Some simple principles to keep in mind are to be <strong>positive</strong>, <strong>humble</strong>, <strong>helpful</strong>, and <strong>enthusiastic</strong>.</p>
    <p>Some simple principles to keep in mind are to be <strong>positive</strong>, <strong>humble</strong>, <strong>helpful</strong>, and <strong>enthusiastic</strong>.</p>
  </section>
  ';

  $dom = new DOMDocument();
  $dom->preserveWhiteSpace = false;
  libxml_use_internal_errors(true);
  $dom->loadHTML($string);  // $string is the block page full / part html       
  $xpath = new DOMXPath($dom);  
  $obj = $xpath->query('//section[@class="post_content"]'); // assume this is the container div that where you want to inject
  $nodes = $obj->item(0)->childNodes;
  $half = $nodes->length / 2;

  $i = 0;
  foreach( $nodes as $node ) {
    if ( $i === $half ) {
      echo "<div class='insert'></div>";
    }
    echo $node->ownerDocument->saveHTML($node);
    $i ++;
  }
$string='1!'
通常,一半的任务是确保你有时间回应你的评论,另一半是记住客户总是对的,你应该谨慎行事

要记住的一些简单原则是积极、谦逊、乐于助人和热情

要记住的一些简单原则是积极、谦逊、乐于助人和热情

'; $dom=新的DOMDocument(); $dom->preserveWhiteSpace=false; libxml\u使用\u内部错误(true); $dom->loadHTML($string);//$string是完整/部分html的块页 $xpath=newdomxpath($dom); $obj=$xpath->query('//section[@class=“post_content”]”);//假设这是要注入的容器div $nodes=$obj->item(0)->childNodes; $half=$nodes->length/2; $i=0; foreach($node作为$node){ 如果($i===$一半){ 回声“; } echo$node->ownerDocument->saveHTML($node); $i++; }
简单地按字符串长度拆分会弄乱输出html。您需要找到要插入广告的容器,并计算子html节点,然后在某些子节点之后,使用广告注入对子注释进行预处理,以重建html

前,

用一种简单的方法,在HTML内容的中间找到一个常量文本标记,并用injection+常量文本标记替换该标记

前,

$cons='Some heading';
$inject='您的广告html/文本';
$string=str_replace=($cons,$inject.$cons,$string);
        $dom = new DOMDocument();
        $dom->preserveWhiteSpace = false;
        libxml_use_internal_errors(true);
        $dom->loadHTML($html);  // $html is the block page full / part html       
        $xpath = new DOMXPath($dom);  

        $obj = $xpath->query('//div[@class="content"]'); // div[@class="content"] - assume this is the container div that where you want to inject
        var_dump($obj); // you will get all the inner content
        echo $htmlString = $dom->saveHTML($obj->item(0));  // you will have all the inner html
        // after this count the all child nodes and inject your advert and reconstruct render the page.
$cons = '<h3 class="heading">Some heading</h3>';
$inject = 'your advert html/text';
$string = str_replace = ($cons, $inject.$cons, $string);