gpt4 book ai didi

php - 使用 PHP 将 HTML 一分为二

转载 作者:行者123 更新时间:2023-11-27 23:19:10 26 4
gpt4 key购买 nike

我正在尝试将一串文本分成两半,注意不要:

  • 断词
  • 破坏 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。您需要找到您想要注入(inject)广告的容器并计算子 html 节点,然后 foreach 子注释以在某些子节点之后用您的广告注入(inject)重建 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内容中间找到一个常量文本标签,用你的注入(inject)+常量文本标签替换这个标签。

例如,

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

关于php - 使用 PHP 将 HTML 一分为二,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41842915/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com