簡體   English   中英

用 PHP 將 HTML 一分為二

[英]Split HTML in two with PHP

我試圖將一串文本分成兩半,注意不要:

  • 打斷的話
  • 打破 html

為了給你一些背景知識,我想拿一篇博客文章並在其中插入一個廣告。

我四處尋找答案,但我能在 SO 上找到的唯一選項建議剝離所有 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 :'(

更新

感謝@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 ++;
  }

簡單地按字符串長度拆分會弄亂輸出 html。 您需要找到要注入廣告的容器並計算子 html 節點的數量,然后 foreach 子注釋以在某些子節點之后使用您的廣告注入重建 html。

前任,

        $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.

或者

以簡單的方式,在 HTML 內容的中間找到一個常量文本標簽,並用您的注入 + 常量文本標簽替換該標簽。

前任,

$cons = '<h3 class="heading">Some heading</h3>';
$inject = 'your advert html/text';
$string = str_replace = ($cons, $inject.$cons, $string);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM