我需要比较2个字符串,并且需要显示特定变量的差异,我在下面创建了代码,但是它不能正常工作,任何人都可以检查我的代码并为此提供适当的解决方案吗?
$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个值,对于其他变量,它没有任何价值
答案 0 :(得分:0)
如果您只想提取图像data-cbp-src
的值,则可以对explode(),strpos()和strpos()进行循环处理,以采取不同的方法:>
$edited = '<img src="" alt="" data-cbp-src="http://cubewp.axcelmedia.com/wp-content/uploads/2019/02/YakiTori.jpg" width="1900" height="1267"></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/02/YakiTori.jpg" class="cbp-lightbox cbp-l-caption-buttonLeft" data-title="View Larger">View Larger</a></div></div></div></div><div class="cbp-l-grid-projects-title">Yaki Tori</div><div class="cbp-l-grid-projects-desc">Yaki Tori</div></div>';
foreach(explode('data-cbp-src="', $edited) as $part) {
if ($end = strpos($part, '"'))
$urls[] = substr($part, 0, $end)
}
很显然,我需要您使用完整的实际代码才能提供完整的实际解决方案,但这将在urls
数组中填充任何元素中data-cpb-src
的所有值。