0

I have a string like below :

this is a testing string. Below is first div with id.
<div id='divid_first_fN8GiCLO_div'></div>
this is a testing string. Below is second div with id.
<div id='divid_second_fN8GiCLO_div'></div>
this is a testing string. Below is third div with id.
<div id='divid_third_fN8GiCLO_div'></div>

Required Result :

this is a testing string. Below is first div with id.
<iframe src="first_fN8GiCLO.html">
this is a testing string. Below is first div with id.
<iframe src="second_fN8GiCLO.html">
this is a testing string. Below is first div with id.
<iframe src="third_fN8GiCLO.html">

I have div id's with me, which need to be changes in iframe source's, Can I do it in some way using preg_match_all to get this done.. as it can be in multiple positions in string.

I need to pick ID form div and put that id in iframe and remove that div. Thanks !

Sachin
  • 113
  • 10

1 Answers1

0

I suggest using preg_replace_callback

#!/usr/bin/php
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);


$in = "this is a testing string. Below is first div with id.
<div id='divid_first_fN8GiCLO_div'></div>
this is a testing string. Below is second div with id.
<div id='divid_second_fN8GiCLO_div'></div>
this is a testing string. Below is third div with id.
<div id='divid_third_fN8GiCLO_div'></div>";

$out = preg_replace_callback('/<div id=.divid_(\w+)_div.><\/div>/',function ($m) {
    return '<iframe src="'.$m[1].'.html">';
},$in);

echo $out;
cske
  • 2,233
  • 4
  • 26
  • 24
  • thanks @cske.. this wonderfully solved my purpose.. and preg_replace_callback is something exactly what I need.. thanks again – Sachin Sep 23 '16 at 06:48