简体   繁体   中英

how to insert <?php echo $image;?> in <img src=“”> tag

i'm trying to modify my PHP code so that i can insert html tag just inside it.the original code was:

<?php 
if( $image ){
?>
<a class="product_image image-woo" href="<?php echo $product_url;?>">
    <?php echo $image;?>
</a>
<?php } ?>

now,i,ve edited in this way:

<?php 
if( $image ){
?>
<img data-src="<?php echo $image;?>" class="something">
<?php } ?>

What,s wrong about?

Its src not data-src

<?php 
 if( $image ){
?>
 <img src="<?php echo $image;?>" class="something">
<?php } ?>

First of all it is src not data-src and you need to echo like this

<?php 
if( $image ){

echo "<img src= '$image' class='something'>";
 } ?>

try this

if($image) {
    $doc = new DOMDocument();
    $doc->loadHTML($image);
    $imgs = $doc->getElementsByTagName('img');
    foreach ($imgs as $img) { ?>
        <img data-src="<?php echo $img->getAttribute("src");?>" class="something"/>
    <?php }
} ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM