php 如何在div类后添加按钮更多链接

5vf7fwbs  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(142)

我有一个内容html

$output = '<div class="elementor-image-box-wrapper">
    <div class="elementor-image-box-content">
        <h3 class="elementor-image-box-title"><a href="#">Industrial</a></h3>
        <p class="elementor-image-box-description">With OT’S tailored workwear solutions, you can op for a comprehensive program that takes care of your requirements entirely or...</p>
    </div>
</div>';

我使用preg_replace在段落类元素or-image-box-description中添加按钮链接

$link = '<a href="#">Read more</a>';
$output = preg_replace( '/^<p class="elementor-image-box-description">(.*)<\/p>$/', '<p class="elementor-image-box-description">$1</p>'.$link, $output );

echo $输出;
但结果不起作用

p5fdfcr1

p5fdfcr11#

I assume you hold all your html in $output and you want to change some parts.
I suggest another way to do it.
create a php file for your component

my-component.php:

<div class="elementor-image-box-wrapper">
    <div class="elementor-image-box-content">
        <h3 class="elementor-image-box-title"><a href="#">Industrial</a></h3>
        <p class="elementor-image-box-description">With OT’S tailored workwear solutions, you can op for a comprehensive program that takes care of your requirements entirely or...</p>
        <?php echo $link; ?>
    </div>
</div>

then you can

$link = '<a href="#">Read more</a>';
ob_start();
require_once('my-component.php');
$output = ob_get_clean();

相关问题