我有2个字符串,我需要比较2个字符串的差值并得到它的值,我确实编写了代码,但是它仅适用于2个变量,在这里,我已经写了完整的代码,有人可以给我解决方案吗,我需要按字符串中定义的变量来赋值,在$template
字符串中定义了变量
[string:class_cover_ratio]
该字符串的值必须与$edited
字符串中的值匹配,因此其值为
class="cbp-item logos cbp-ratio-even" data-cbp-coverratio="4:3"
对于其他变量,比较字符串并为其获取值,这是我的代码
$template = '<div[string:class_cover_ratio]><div[string:class_2]><div class="cbp-caption-defaultWrap"><img src="data:image/gif;base64,R0lGODlhAQABAPAAAP///////yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="" data-cbp-src="[image:thumbnail_name]" width="[string:large_width]" height="[string:large_height]"></div><div class="cbp-caption-activeWrap"><div class="cbp-l-caption-alignCenter"><div class="cbp-l-caption-body"><a href="[image:large_name]" class="cbp-lightbox cbp-l-caption-buttonLeft" data-title="[string:title]">[string:view_larger_text]</a></div></div></div></div><div class="cbp-l-grid-projects-title">[string:dish_name]</div><div class="cbp-l-grid-projects-desc">[string:dish_description]</div></div>';
$edited = '<div class="cbp-item logos cbp-ratio-even" data-cbp-coverratio="4:3"><div class="cbp-caption" style=""><div class="cbp-caption-defaultWrap"><img src="data:image/gif;base64,R0lGODlhAQABAPAAAP///////yH5BAAAAAAALAAAAAABAAEAAAICRAEAOw==" alt="" data-cbp-src="http://cubewp.axcelmedia.com/wp-content/uploads/2019/01/Ankimo-thum.jpg" width="400" height="300"></div><div class="cbp-caption-activeWrap"><div class="cbp-l-caption-alignCenter"><div class="cbp-l-caption-body"><a href="http://cubewp.axcelmedia.com/wp-content/uploads/2019/01/Ankimo.jpg" class="cbp-lightbox cbp-l-caption-buttonLeft" data-title="Monkfish Liver, lightly garnished">View Larger</a></div></div></div></div><div class="cbp-l-grid-projects-title">Ankimo</div><div class="cbp-l-grid-projects-desc">Monkfish Liver, lightly garnished</div></div>';
function test($edited, $template) {
$gottenValues = [];
preg_match_all('/\[.*\]/U', $template, $names);
$names = $names[0];
$templateParts = preg_split('/\[.*\]/U', $template);
foreach ($templateParts as $index => $part) {
if ($index === 0) {
$part = trim($part);
$edited = trim($edited);
$edited = substr($edited, strlen($part));
continue;
}
$edited = explode($part, $edited);
$gottenValues[trim($names[$index - 1], '[]')] = $edited[0];
if (isset($edited[1])) {
$edited = $edited[1];
} else {
$edited = "";
}
}
echo '<pre>';
print_r($gottenValues);
die;
}
test($edited, $template);
我的输出低于输出
Array
(
[string:class_cover_ratio] => class="cbp-item logos cbp-ratio-even" data-cbp-coverratio="4:3"
[string:class_2] => class="cbp-caption" style=""
[image:thumbnail_name] =>
[string:large_width] =>
[string:large_height] =>
[image:large_name] =>
[string:title] =>
[string:view_larger_text] =>
[string:dish_name] =>
[string:dish_description] =>
)
在输出中,您可以看到我仅获得2个变量值,其他值对我不起作用,有人可以帮我吗?