我想从下面的字符串中获得一个ID数组。
[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]
理想情况下,我想看看这个字符串,在图像中得到一个INT值的数组。例如
array("3057", "2141", "234");
找到图像值并将其分解以接收阵列
$str = '[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]';
if (preg_match('/imagess*=s*"([^"]+)"/', $str, $m)) {
$res = explode(',', $m[1]);
print_r($res);
}
使用explode
和strpos
函数的另一个解决方案:
$str = '[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]';
foreach (explode(" ", $str) as $v) {
if (strpos($v, "images=") === 0) {
$result = explode(",", explode('"', $v)[1]);
break; // avoids redundant iterations
}
}
print_r($result);
输出:
Array
(
[0] => 3057
[1] => 2141
[2] => 234
)