stripos - Filter a string in PHP

887

I'm looking to get an array of ID's from the following string.

[vc_gallery type="flexslider_fade" interval="3" images="3057,2141,234" onclick="link_image" custom_links_target="_self" img_size="large"]

Ideally, i'd like to look at this string and get an array of the INT values within images. e.g.

array("3057", "2141", "234");

114

Answer

Solution:

find images value and explode it to receive array

$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('/images\s*=\s*\"([^\"]+)\"/', $str, $m)) {
   $res = explode(',', $m[1]);
   print_r($res);
}
921

Answer

Solution:

Another solution usingexplode andstrpos functions:

$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);

The output:

Array
(
    [0] => 3057
    [1] => 2141
    [2] => 234
)

People are also looking for solutions to the problem: How to make a list out of a 3D array inner data PHP

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.