Monnster
Monnster

Reputation: 645

How to replace part of multiple html tag with different content depends on the given data

I need to replace multiple html tags. sample code below :

$html = "<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  <visualcontent imgid='1' type='jpg'></visualcontent>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  <visualcontent imgid='2' type='png'></visualcontent>
</p>";

The output I needed to have is:

$html = "<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  <img src='1.jpg'>


  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  <img src='2.png'>
</p>";

If you see :

<visualcontent imgid='x' type='x'></visualcontent> 

was replaced with image tag. I'm clueless how to do this. please help.

Thanks

Upvotes: 0

Views: 30

Answers (1)

Alex Andrei
Alex Andrei

Reputation: 7283

You can easily do this with Simple HTML DOM Parser

Here's how:

<?php
include('simple_html_dom.php');

$htmlString = "<p>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  <visualcontent imgid='1' type='jpg'></visualcontent>
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book.
  <visualcontent imgid='2' type='png'></visualcontent>
</p>";

$html = str_get_html($htmlString);

foreach($html->find('visualcontent') as $v){
    $src = $v->imgid;
    $type = $v->type;

    $v->outertext = '<img src="'. $src .'.'.$type.'" />';
}

$output = $html->save();

print $output;

$html->clear();
unset($html);

Will give you exactly what you are after:

<p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. <img src="1.jpg" /> Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. <img src="2.png" /> </p>

Upvotes: 1

Related Questions