使用 preg_match_all 获取 Div id

Get Div id using preg_match_all

我有如下字符串:

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>

要求的结果:

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">

我有 div id,这需要在 iframe 源中进行更改,我可以使用 preg_match_all 以某种方式完成吗这完成了..因为它可以在字符串中的多个位置。

我需要选择 ID 形式 div 并将该 ID 放入 iframe 并删除 div。 谢谢!

我建议使用 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;